我有以下表格
1. user table containing
userid lastlatitude lastlongitude lastlocation
---------------------------------------------------
1 38.555605 -121.468926 California
2 30.4518 -84.27277 Florida
2. locations table containing
location_id latitude longitude location_name
----------------------------------------------------
1 38.7547 -140.7878892622 abc
2. 40.7547 -111.895672622 New asdsd
3 41.xxx xxxxxxxx xxxxxx
我需要获得以下输出: 如果我通过用户lat和long我应该得到最近的20个位置,这些位置存储在我的位置表
中答案 0 :(得分:0)
首先,您需要计算距离。如果有帮助,请查看http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL。
编辑:
与http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL一样,以下查询可能符合您的要求:
set @orig_lat=122.4058; // pass users lat
set @orig_lon=37.7907; // pass users long
SELECT *,
3956 * 2 * ASIN(SQRT( POWER(SIN((@orig_lat -
abs(
dest.latitude)) * pi()/180 / 2),2) + COS(@orig_lat * pi()/180 ) * COS(
abs
(dest.latitude) * pi()/180) * POWER(SIN((@orig_lon – dest.longitude) * pi()/180 / 2), 2) ))
as distance FROM locations dest ORDER BY distance limit 20;