0

我们在地图上有一个天气标记表,其中每个标记类型是风或云。

对于每个云标记,我们希望找到最近的风标记的“WindDir”字段值。似乎自联接可以解决问题,但事实证明构造查询很困难。

下面是我们不正确的伪 SQL。我们知道距离值将是我们想要的。我们只需要如何使查询为每个云标记找到最近的风标记,这样我们就可以得到风标记的“WindDir”值。

SELECT w1.ID AS ID, w1.Lat AS Lat, w1.Lng AS Lng, 
       w2.WindDir AS WindDir, 
       MIN(SQRT(POWER(ABS(w1.Lng - w2.Lng), 2) + POWER(ABS(w1.Lat - w2.Lat), 2))*60) AS Distance 
FROM Weather w1 WHERE w1.Marker="Cloud"
LEFT JOIN Weather w2 WHERE w2.Marker="Wind"
USING ID  

对于制作有效版本的任何建议,我们将不胜感激!

-肯

4

1 回答 1

0

You're pretty close (basically just reordered for syntax)

SELECT w1.ID AS ID, w1.Lat AS Lat, w1.Lng AS Lng, 
       w2.WindDir AS WindDir, 
       (SQRT(POWER(ABS(w1.Lng - w2.Lng), 2) + POWER(ABS(w1.Lat - w2.Lat), 2))*60) AS Distance 
FROM Weather w1 LEFT JOIN Weather w2 USING (ID)
WHERE w1.Marker="Cloud" AND w2.Marker="Wind"
ORDER BY Distance DESC
LIMIT 1

Assuming you just want the closest result, I added a LIMIT 1 to restrict what's returned.

On a side note, this method of finding distance between two lat/lon pairs is unlikely to give you good results for even fairly small changes. There are methods for approximating these distances...

于 2011-01-17T05:26:44.350 回答