Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:10:55+00:00 2026-06-14T19:10:55+00:00

I have a charities table with fields: charity, postcode and a postcodes table with

  • 0

I have a charities table with fields: charity, postcode
and a postcodes table with fields: postcode, lat, lng

I want to POST a postcode from a web page and find the nearest charities

I’m something of a mysql beginner so I’m a bit lost, but I’ve been trying various ideas with joins and sub queries none of which work (I either get syntax errors or ‘Operand should contain 1 column’ with variations on the code below) I’ve got

Select charity,postcode,
 ( 
   (Select lat as lat2, lng as lng2
    from postcodes
    where postcode='WN8'
    )

3959 * acos( cos( radians(lat2) ) * cos( radians( lat ) ) * 
cos( radians( lng ) - radians(lng2) ) + 
sin( radians(lat2) ) * sin( radians( lat ) ) ) 
  )
AS distance 
FROM postcodes  
JOIN   Charities on charities.postcode=postcodes.postcode
HAVING distance < 30 ORDER BY distance LIMIT 0 , 30;

I’ve seen lots of examples on here where lat2 and lng2 are obtained from posted values but not from a table in the db.

p.s 'where postcode='WN8' in the example is just for testing

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:10:56+00:00Added an answer on June 14, 2026 at 7:10 pm

    Not sure what error you are getting with the above SQL.

    However try this minor tweak and let us know what errors you get

    SELECT charity, postcode,
    (3959 * acos( cos( radians(CustPostcode.lat) ) * cos( radians( postcodes.lat ) ) * 
    cos( radians( postcodes.lng ) - radians(CustPostcode.lng) ) + 
    sin( radians(CustPostcode.lat) ) * sin( radians( postcodes.lat ) ) ) 
      ) AS distance 
    FROM postcodes  
    INNER JOIN Charities ON charities.postcode=postcodes.postcode
    CROSS JOIN  (SELECT lat, lng FROM postcodes WHERE postcode='WN8') CustPostcode
    HAVING distance < 30 
    ORDER BY distance 
    LIMIT 0 , 30;
    

    If you want to know the nearest 30 postcodes and the distances from each charity then something like this would do the job (not tested so excuse any typos).

    SELECT charity, Charities.postcode, Postcodes.postcode, PostcodeDistance.distance
    FROM Charities
    CROSS JOIN Postcodes
    INNER JOIN (SELECT PC1.postcode AS postcode1, PC2.postcode AS postcode2, (3959 * acos( cos( radians(PC1.lat) ) * cos( radians( PC2.lat ) ) * 
    cos( radians( PC2.lng ) - radians(PC1.lng) ) + 
    sin( radians(PC1.lat) ) * sin( radians( PC2.lat ) ) ) 
      ) AS distance 
    FROM postcodes PC1
    CROSS JOIN postcodes PC2) PostcodeDistance
    ON Charities.postcode = PostcodeDistance.postcode1
    AND Postcodes.postcode = PostcodeDistance.postcode2
    HAVING distance < 30 
    ORDER BY distance 
    LIMIT 0 , 30;
    

    This should find you the charities within 30 miles

    SELECT charity, Charities.postcode, PostcodeDistance.distance
    FROM Charities
    INNER JOIN (
    SELECT PC2.postcode AS postcode2, (3959 * acos( cos( radians(PC1.lat) ) * cos( radians( PC2.lat ) ) * 
    cos( radians( PC2.lng ) - radians(PC1.lng) ) + 
    sin( radians(PC1.lat) ) * sin( radians( PC2.lat ) ) ) 
    ) AS distance 
    FROM postcodes PC1
    CROSS JOIN postcodes PC2
    WHERE PC1.postcode='WN8'  
    ) PostcodeDistance
    ON Charities.postcode = PostcodeDistance.postcode2
    WHERE PostcodeDistance.distance < 30 
    ORDER BY PostcodeDistance.distance 
    LIMIT 0 , 30;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have a table which holds the names of loads of charities in the
I have an object named Charity and I want to make sure that each
Have a painfully simple blog Post creator, and I'm trying to check if the
have a nice day. I got problem when trying to create an image from
Have finally got a responsive site working (of a fashion). What I want to
Have a look at the following code to find X^y. /* Find exponent in
I need to get 5 random records from a table plus a further record
I have a site that need to get some data from a different sit
When a user clicks on a link on my page, I want to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.