Skip to content

Instantly share code, notes, and snippets.

@kushniryb
Last active January 31, 2018 09:45
Show Gist options
  • Save kushniryb/94baf4afe1d6b842dee8a66093d1f6dc to your computer and use it in GitHub Desktop.
Save kushniryb/94baf4afe1d6b842dee8a66093d1f6dc to your computer and use it in GitHub Desktop.
MySQL Points Within a Distance
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