0

我有以下表格

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 个位置,这些位置存储在我的位置表中

4

1 回答 1

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;
于 2012-07-06T08:50:35.503 回答