Skip to content

Instantly share code, notes, and snippets.

@codyogden
Created May 25, 2016 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyogden/bd667b27c99f35e6926ebe28e912b5d2 to your computer and use it in GitHub Desktop.
Save codyogden/bd667b27c99f35e6926ebe28e912b5d2 to your computer and use it in GitHub Desktop.
A hacked together query for finding nearest locations in a database. :oldlat & :oldlng are the center point, :dist is number of miles from center point.
SELECT *, ( 3959 * acos( cos( radians( :oldlat ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( :oldlng ) ) + sin( radians(:oldlat) ) * sin( radians( latitude ) ) ) ) AS distance FROM agency HAVING distance < :dist ORDER BY distance LIMIT :numb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment