1

次のようなユーザーのリストを含むテーブルがあります。

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 

しかし、その後、ユーザーの緯度/経度を各結果のサブクエリに渡すことができないことがわかりました。

ただし、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 を渡すことができない場合、サブクエリでこれをどのように使用できますか?

どんな助けでも、大歓迎です。

4

0 に答える 0