Geo-Distance-Search-with-MySQL

https://stackoverflow.com/a/26407391/4074710

set @orig_lat=52.5200070; set @orig_lon=13.4049540; set @dist=5;

SELECT
  *, (
    3959 * acos (
      cos ( radians(@orig_lat) )
      * cos( radians( lat ) )
      * cos( radians( lon ) - radians(@orig_lon) )
      + sin ( radians(@orig_lat) )
      * sin( radians( lat ) )
    )
  ) AS distance
FROM my_table
HAVING distance < @dist
ORDER BY distance
LIMIT 0 , 20;

 

Categories: Database