分配的查询变量无法识别

时间:2013-12-23 07:46:01

标签: mysql sql

我有一个项目表,每个项目都有纬度和经度值,以便我计算距离。

我正在尝试以下操作,但它会返回: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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;