Created
October 28, 2018 20:08
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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