mysql - 如何在 WHERE 中使用 NULL 列?
问题描述
我对没有列的查询有一些问题,所以当我尝试使用 WHERE 进行查询时。我收到有关该表没有此列的错误,
SELECT NULL AS users FROM test WHERE users = 1
我知道该查询没有任何意义。但是我需要对表中不存在的列执行 WHERE
解决方案
利用IS NULL
select column_name
from table_name
where column_name is null;
或者IS NOT NULL
select column_name
from table_name
where column_name is not null;
在这里你可以看到一个DEMO
然后,如果您的表中没有该列,您可以像这样使用它:
with cte as
(select null as users
, column_name
from table_name)
select * from cte
where users is null; -- you can replace this row with "where users = 1;"
这是该示例的演示