Options
$lat = '25.7742658';
$lng = '-80.1936589';
$miles = 30;
Query
SELECT *,
( 3959 * acos( cos( radians($lat) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians($lng) )
+ sin( radians($lat) )
* sin( radians( lat ) ) ) ) AS distance
FROM locations
HAVING distance < $miles
ORDER BY distance
LIMIT 0, 20
I have a database table with 4 columns:
- unique id
- city name
- latitude (lat)
- longitude (lng)
I'm using the query on top to return locations that are within a specified number of miles from the specified coordinates. It seems to work but I'm not sure how accurate it is. I'm curios to know whether the query is good or if you have a better solution.
if you want calculate distance from to point or find nearly place to one pointin mysql, you can use "MySQL Spatial Functions", but it is enable in MySQL 5.6. for more information you can read this article:
https://www.percona.com/blog/2013/10/21/using-the-new-mysql-spatial-functions-5-6-for-geo-enabled-applications/
or this PDF:
http://www.arubin.org/files/geo_search.pdf
that looks like the correct great circle distance query.
what are you concerned with wrt accuracy?