NOT IN之后的子查询为什么不能包含NULL值

NOT IN后面的子查询有记录为NULL,主查询查不到记录
select * from emp where id not in (1, 2, null);
未选定行
因为这个查询可以理解为
select * from emp where id <> 1 and id <> 2 and id <> null;
由于NULL值比较特殊,不能参与比较运算符(可以单独使用is null 或 is not null来判断),导致条件不成立,因此查询不出来数据。
如果想查到涉及1,2的记录,那么可以使用not exists。如:--假设 select id from dep tt的结果值就为(1,2,null)
select * from emp t where t.id not exists (select 1 from dep tt where t.id = tt.id);

 

posted @ 2023-04-18 15:59  Mrzxs  阅读(102)  评论(0编辑  收藏  举报