not in 和 not exists 区别

来源:互联网 发布:淘宝led散热片 编辑:程序博客网 时间:2024/06/07 01:09

select * from TableA a where not exists (select b.AId from TableB b where b.AId=a.id)等价

select * from TableA a where a.id not in(select b.AId from TableB b where b.AId is not null and b.AId !='')

注意:

如果去掉红色部分约束条件,当查询集合(select b.AId from TableB b)中没有null和空字符串时,

select * from TableA a where a.id not in(select b.AId from TableB b)查询结果正确

若查询集合(select b.AId from TableB b)中 有null或空字符串时

select * from TableA a where a.id not in(select b.AId from TableB b)查询结果为null

原创粉丝点击