mysql - 为什么我在 'where clause' 中使用 MySQL 的子查询别名得到未知列?

标签 mysql sql

当我尝试为子查询提供别名然后在 Where 子句中引用它时,我收到一条错误消息:“where 子句”中的未知列“point_status”

SELECT goals.id, goals.status, 
ifnull((SELECT point_status FROM pts WHERE point_id = ?), goals.status) as point_status
FROM   goals
WHERE  goals.name = ? AND point_status > 1

我不明白为什么它不允许我使用这个别名。是否可以通过不修改查询的功能以某种方式在 where 子句中使用该别名?

最佳答案

It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed.

这意味着您不能在 where 子句中使用别名。

尝试将 where 替换为 having

关于mysql - 为什么我在 'where clause' 中使用 MySQL 的子查询别名得到未知列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27989866/

相关文章:

mysql - 为什么我收到 "Could not find table ' users'"错误?

php - 从另一个 php 文件中检索数据

java - JDBC批量插入——控制插入速度

sql - 如何构造此 Oracle SQL 查询?

php - 使用 PHP 将数据库行放入数组中?

php - 突出显示当前页码 PHP 分页

mysql - AUTO_INCREMENT 在我的案例中是否正确实现?

php - Wordpress mysql_fetch_array() 期望参数 1 为资源错误

sql - 可扩展性无关紧要时的 NoSQL 与 SQL

sql服务器: include column that is not part of an aggregate function or Group by clause