表T1(C1 int)
C1
------
10
20
30
执行查询1:select * from T1 where C1 in(10, 20,null)
返回:
C1
------
10
20
结果没问题
如果执行查询2:select * from T1 where C1 not in(10, 20,null)则返回空结果集
原因如下:
sql对in操作符在逻辑上将其处理为or操作,查询1转换为select * from T1 where (C1=10 or C1=20 or C1=null),查询2转换为select * from T1 where not(C1=10 or C1=20 or C1=null)
在SQL中,true or null的结果是true,而false or null的结果是null!当在结果中有一个null时,null就会延续下去
在not in列表中可能有null值时必须注意