SQL中当null加入比较

来源:互联网 发布:医疗大数据前景 编辑:程序博客网 时间:2024/05/17 01:19

select * from device where id NOT IN(select dId from user)



User表中dId字段绑定device表中id字段 目标是获取到还没有被绑定的id


通过上面的语句获取不到结果,一直是空


百度了一下,好像是因为user中有条目的dId为null,not in的条件中有null就会什么都查不出来。

具体的原因可以看 http://www.cnblogs.com/killkill/archive/2010/09/04/1817266.html 写的不错


结果来说 SQL改成以下语句就能正常了


select * from device where id NOT IN(select dId from user where dId is not null)


0 0