为什么以下查询不起作用?Mysql抱怨z-我不能在WHERE子句中使用别名吗?
SELECT x + y AS z, t.* FROM t WHERE x = 1 and z = 2
我得到的错误是:
Error Code : 1054 Unknown column 'z' in 'where clause'
http://dev.mysql.com/doc/refman/5.0/en/problems-with- alias.html
标准SQL不允许在WHERE子句中引用列别名。之所以施加此限制,是因为在评估WHERE子句时,可能尚未确定列值。例如,以下查询是非法的: SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;
标准SQL不允许在WHERE子句中引用列别名。之所以施加此限制,是因为在评估WHERE子句时,可能尚未确定列值。例如,以下查询是非法的:
SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;
试试这个,代替:
SELECT x + y AS z, t.* FROM t WHERE x = 1 HAVING z = 2;