这个问题的根源在于null,众所周知,当判断一个值是否为null的时候,sql要用is null 或者is not null, 在SQL中,Null值并不是一个值,而是表示特定含义,其所表示的含义是“Unknow”,可以理解为未定义或者未知,因此任何与Null值进行比对的二元操作符结果一定为Null,包括Null值本身
解决:
select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where CustomerID)
改为
select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where CustomerID is not null)
注意:
应尽量避免使用not in,not in结果不准确,性能效率低。可以采用not exists方案替代
select ID from SmartCustomer a where not exists (select ID from SmartPromoter b where a.ID=b.CustomerID)