SQL-选择包含至少一个NULL值的列?

时间:2019-12-11 20:19:33

标签: mysql sql presto

如何过滤对至少具有一个null值的特定列进行过滤的输出的位置?

因此。在状态列中至少有一个NULL值的用户?

ID    |         Status
B        2017-10-03 08:00:00
B                NULL
G        2017-10-03 08:00:00
G        2017-10-03 08:00:00
G        2017-10-03 08:00:00
B        2017-10-03 08:00:00
D        2017-10-03 08:00:00
D                NULL
D                NULL

3 个答案:

答案 0 :(得分:1)

您可以使用聚合:

select id
from t
group by id
having count(*) <> count(status);

如果要原始行,可以使用exists

select t.*
from t
where exists (select 1
              from t t2
              where t2.id = t.id and t2.status is null
             );

答案 1 :(得分:1)

我认为这是最简单,最快的方法。

select id
from t
where status is null
group by id;

答案 2 :(得分:0)

查询:

select id
from tablename
where status is null

使用id返回所有包含至少1个status的{​​{1}}。
所以像这样使用它:

null

如果只需要select * from tablename where id in (select id from tablename where status is null)

id