在工作中执行以下SQL:
Select * from table1 A where A.col1 not in ( select col1 from table2 B )
发现当B表中存在col1字段为空的数据时, 查询结果集是空。
在网上查询了一番,原来真的存在这个问题:
举例说明: 5 not in ('2','3','4',' ') 等价于 not (false or false or false or null)
=> not null 即 查询不出结果集来。
所以如果非要用not in :
Select * from table1 A where A.col1 not in ( select col1 from table2 B where B.col1 is not null )
具体分析可以参考: