如何在连接查询中查找最近的位置(纬度/经度)

时间:2012-05-10 00:28:53

标签: mysql geolocation geocoding geospatial computational-geometry

我有一个包含用户列表的表,看起来像;

Users..  ( id, username, lat, long )

我有一个如下所示的位置表:

Locations.. ( id, name, lat, lng, bound_ne_lat, bound_ne_lng, bound_sw_lat, bound_sw_lng

我正在尝试做的事情就像是;

SELECT Users.*, Locations.name as NearestTown
FROM Users
INNER JOIN Locations ON ( .. lookup nearest town from Locations based on Users lat lng coords, return only the nearest result .. )

我确实考虑过做某种子查询,比如;

INNER JOIN Locatios ON Locations.id IN (SELECT id FROM Locations ORDER BY (..distance query) desc limit 1 

但后来我发现我无法将用户lat / lng传递给每个结果的子查询。

然而,我使用的当前公式来计算2点之间的距离;

 (3956 * 2 * ASIN(SQRT( POWER(SIN((@sourceLat - table.lookupLat) *  pi()/180 / 2), 2) +COS(@sourceLat * pi()/180) * COS(table.lookupLat * pi()/180) * POWER(SIN((@sourceLng - table.lookupLng) * pi()/180 / 2), 2) ))) as Distance,

但是,当我无法为每个结果传递@sourceLat和@sourceLng时,如何在子查询[如果是最佳选项]中使用它?

任何帮助,非常感谢。

0 个答案:

没有答案