2011-06-01 62 views
1

這是我使用緯度經度值計算距離的查詢。如何獲得距離<500?

SELECT mm.mem_id, mm.profilenam, mm.photo_thumb, mm.city, mm.zip, mm.country, mm.state, (3956 *2 * ASIN(SQRT(POWER(SIN((34.1012181 - ABS(latitude)) * PI() /180 /2) , 2) + COS(34.1012181 * PI() /180) * COS(ABS(latitude) * PI() /180) * POWER(SIN((
ABS(- 118.325739) - ABS(longitude)) * PI() /180 /2) , 2))) 
) AS distance 
FROM members AS mm 
WHERE mm.profile_type = 'C' 
ORDER BY distance 
LIMIT 0 , 10 

我想距離< 500,但是當我把查詢 ... < 500距離,這是行不通的 我怎樣才能得到最終的查詢。

+1

爲什麼你需要採取緯度的絕對值?看起來不對我。特別是對於所有緯度值,由於「COS(ABS(緯度)* PI()/ 180)='COS(緯度* PI()/ 180)」。 – Eric 2011-06-01 09:31:05

回答

2

你試過

SELECT mm.mem_id, mm.profilenam, mm.photo_thumb, mm.city, mm.zip, mm.country, mm.state, (3956 *2 * ASIN(SQRT(POWER(SIN((34.1012181 - ABS(latitude)) * PI() /180 /2) , 2) + COS(34.1012181 * PI() /180) * COS(ABS(latitude) * PI() /180) * POWER(SIN((
ABS(- 118.325739) - ABS(longitude)) * PI() /180 /2) , 2))) 
) AS distance 
FROM members AS mm 
WHERE mm.profile_type = 'C' 
HAVING distance<500 
0

由於這是一個派生列,你需要使用一個HAVING子句

SELECT mm.mem_id, mm.profilenam, mm.photo_thumb, mm.city, mm.zip, mm.country, mm.state, (3956 *2 * ASIN(SQRT(POWER(SIN((  34.1012181 - ABS(latitude)) * PI() /180 /2) , 2) + COS(34.1012181 * PI() /180) * COS(ABS(latitude) * PI() /180) * POWER(SIN((
ABS(- 118.325739) - ABS(longitude)) * PI() /180 /2) , 2))) 
) AS distance 
FROM members AS mm 
WHERE mm.profile_type = 'C' 

HAVING distance < 500 

ORDER BY distance 
LIMIT 0 , 10