Last active
January 31, 2018 09:45
-
-
Save kushniryb/94baf4afe1d6b842dee8a66093d1f6dc to your computer and use it in GitHub Desktop.
MySQL Points Within a Distance
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
SET @radius = 30; | |
SET @lat = 49.536252845380; | |
SET @lng = 23.786006985056; | |
SET @earth_radius = 6371; | |
SELECT | |
shops.name AS 'Shop Name', shops.id AS 'Shop ID', addresses.lat AS 'Address Lat', addresses.lng AS 'Address Lng', address AS 'Address Line', ( | |
@earth_radius * acos ( | |
cos ( radians(@lat) ) | |
* cos( radians( lat ) ) | |
* cos( radians( lng ) - radians(@lng) ) | |
+ sin ( radians(@lat) ) | |
* sin( radians( lat ) ) | |
) | |
) AS Distance | |
FROM addresses | |
INNER JOIN shops ON addresses.shop_id = shops.id | |
HAVING Distance <= @radius | |
ORDER BY Distance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment