-1

当我尝试执行下面的 sql 时,我收到一条错误消息,指出 Invalid Column Name distance。我没有在我的 sql 中发布那么多,所以我不确定问题是什么。在此先感谢您的帮助!

SELECT ID, 
  3959 * ACOS(COS(RADIANS(34.743305)) 
     * COS(RADIANS(Lattitude)) * COS(RADIANS(Longitude) - RADIANS(- 85.059586)) 
     + SIN(RADIANS(34.743305)) * SIN(RADIANS(Lattitude))) AS distance
FROM RetailerRegistration
HAVING (distance < 25)
4

3 回答 3

5

问题是您试图引用您在 SELECT 列表中创建的别名,我建议使用子查询,然后使用您的别名:

select ID, distance
from
(
  SELECT ID, 
    3959 * ACOS(COS(RADIANS(34.743305)) 
       * COS(RADIANS(Lattitude)) * COS(RADIANS(Longitude) - RADIANS(- 85.059586)) 
       + SIN(RADIANS(34.743305)) * SIN(RADIANS(Lattitude))) AS distance
  FROM RetailerRegistration
) d
where distance < 25;

处理查询的逻辑顺序是:

  1. 在哪里
  2. 通过...分组
  3. 拥有
  4. 选择
  5. 订购方式

因此,distance尚未为您的 WHERE 子句定义别名。解决此问题的一种方法是使用子查询。

于 2013-09-25T14:38:43.307 回答
0

having与 结合使用group by。我认为您将其与...混淆了where。另外,请尝试将公式放入where子句中,而不是引用别名。

SELECT ID, 
  3959 * ACOS(COS(RADIANS(34.743305)) 
     * COS(RADIANS(Lattitude)) * COS(RADIANS(Longitude) - RADIANS(- 85.059586)) 
     + SIN(RADIANS(34.743305)) * SIN(RADIANS(Lattitude))) AS distance
FROM RetailerRegistration
WHERE
  3959 * ACOS(COS(RADIANS(34.743305)) 
     * COS(RADIANS(Lattitude)) * COS(RADIANS(Longitude) - RADIANS(- 85.059586)) 
     + SIN(RADIANS(34.743305)) * SIN(RADIANS(Lattitude))) < 25
于 2013-09-25T14:33:34.197 回答
-1

您没有通过..尝试这样做来使用任何组:

SELECT ID, 3959 * ACOS(COS(RADIANS(34.743305)) * COS(RADIANS(Lattitude)) * COS(RADIANS(Longitude) - RADIANS(- 85.059586)) + SIN(RADIANS(34.743305)) * SIN(RADIANS(Lattitude))) AS distance FROM RetailerRegistration where (distance < 25)

有子句用于对一组行而不是单个行施加条件

希望这可以帮助....

于 2013-09-25T14:32:57.627 回答