SELECT ( 3959 * acos( cos( radians(origin_lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(origin_long) ) + sin( radians(origin_lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM zip_codes HAVING distance <= _miles ORDER BY zip;
Wednesday, October 9, 2013
SQL to find distance from latitude/longitude
This is a quick post, mostly so i never forget this. A guy I work with either wrote this or found it on the internet. If you have a database of addresses with lat/long pairs for each entry, this SQL statement will find the distance (I think in miles) from a given lat/long.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment