2013-12-23 46 views
1

我有一個項目表,每個項目都有經度和緯度值,可以計算距離。分配的查詢變量不被識別

我嘗試以下,但它返回:Unknown column distance in where clause

select (3959 * acos(cos(radians('53.993252')) 
* cos(radians(latitude)) 
* cos(radians(longitude) 
- radians('-0.432470')) 
+ sin(radians('53.993252')) 
* sin(radians(latitude)))) AS distance from items where distance < 1000 
+0

[使用列別名WHERE的MySQL條款(http://stackoverflow.com/questions/942571/using-column-alias-in-where-clause- of-mysql-query-produce-an-error) – valex

回答

2
select (3959 * acos(cos(radians('53.993252')) 
* cos(radians(latitude)) 
* cos(radians(longitude) 
- radians('-0.432470')) 
+ sin(radians('53.993252')) 
* sin(radians(latitude)))) AS distance from items having distance < 1000 

WHERE犯規看到別名使用HAVING

+0

嗯,我很害怕這個問題,這導致我面臨更大的問題......我會把它作爲一個單獨的問題發佈 - 非常感謝! –

1

您可以使用子查詢 -

SELECT * FROM (
    SELECT 
    (3959 * ACOS(COS(RADIANS('53.993252')) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS('-0.432470')) + SIN(RADIANS('53.993252')) * SIN(RADIANS(latitude)))) AS distance 
    FROM items)t 
WHERE distance < 1000;