SELECT 字段作为 mytest WHERE mytest 为 NULL - 字段 "mytest" 未找到?

SELECT field as mytest WHERE mytest IS NULL - field "mytest" not found?

我试图在 where 子句中使用选定字段的命名别名,但数据库抱怨无法找到该字段。

查询:

SELECT somefunction(myfield) as mytest, myotherfield as mytest2
FROM database.table
WHERE mytest IS NULL OR mytest2 IS NULL

期望: 数据库将简单地测试 somefunction(myfield)mytest 的结果是否为 NULL

结果:数据库找不到mytestmytest2

有什么我遗漏的吗?为什么这样的东西不起作用?

SELECT somefunction(myfield) as mytest, myotherfield as mytest2 FROM table WHERE mytest IS NULL OR mytest2 IS NULL

编辑:抱歉 table。

可能重复:

Using column alias in WHERE clause of MySQL query produces an error

答案:

您只能在 GROUP BY、ORDER BY 或 HAVING 子句中使用列别名。

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.

复制自http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html