存储过程错误未知列 mySQL

Stored Procedures error unknown column mySQL

当我尝试 运行 存储过程时出现此错误。

mysql> call get_nearby;

ERROR 1054 (42S22): Unknown column 'longRadius' in 'where clause'

这里是 sql 创建存储过程:

CREATE PROCEDURE get_nearby() BEGIN SET @lat = 10; set @long = 12;
SELECT id, 
  @long - radius / abs(cos(radians(@lat)) * 69) as longRadius, 
  (radius/69) as latRadius
FROM area
WHERE longitude between @long + longRadius and @long - longRadius
AND latitude between @lat + latRadius and @long - latRadius;
END

我不知道为什么它不能识别指定的列名。目前,我在开始时设置了@lat和@long以简化测试。

如有任何帮助,我们将不胜感激。

您不能在 where 子句中使用列别名。您可以将该 where 子句转换为 "having" 子句,这样您就可以使用别名了。您可以获得更多信息 here and here.

来自 Mysql 文档:

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

您也可以在 where 子句中重述计算,或使用子查询。