If you have a database of zipcodes and their latitudes and longitudes, you can use a version of this query to get the geographically closest zipcodes:

SELECT b.zipcode, b.city, b.state, b.latitude, b.longitude,
ACOS(SIN(RADIANS(a.latitude))
* SIN(RADIANS(b.latitude)) +
COS(RADIANS(a.latitude))
* COS(RADIANS(b.latitude))
* COS(RADIANS(a.longitude - b.longitude))) as distance
FROM zipcodes.zip_to_latlong a,
zipcodes.zip_to_latlong b
WHERE a.zipcode=?
ORDER BY distance
LIMIT 20

The “distance” there is…I dunno…radians? I think the original is assuming the points are on a sphere, and converts from radians to degrees to miles using the 1.1515 statue miles per nautical mile standard.

### Like this:

Like Loading...

*Related*

## Leave a Reply