Skip to content

Instantly share code, notes, and snippets.

@obrassard
Created October 28, 2018 20:08
Show Gist options
  • Save obrassard/4b377f082cf539163a1f072091b172ab to your computer and use it in GitHub Desktop.
Save obrassard/4b377f082cf539163a1f072091b172ab to your computer and use it in GitHub Desktop.
SQL statement to find locations within a X distance of another location (with lat / lng)
# https://gis.stackexchange.com/questions/31628/find-points-within-a-distance-using-mysql
SET @lat = 45.536264;
SET @long = -73.493025;
SELECT
id, name, (
6371 * acos (
cos ( radians(@lat) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(@long) )
+ sin ( radians(@lat) )
* sin( radians( lat ) )
)
) AS distance
FROM locations
HAVING distance < 1.0
ORDER BY distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment