not in查询不出数据问题


   

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)

这个问题的根源在于null,众所周知,当判断一个值是否为null的时候,sql server要用is null 或者is not null,  在SQL Server中,Null值并不是一个值,而是表示特定含义,其所表示的含义是“Unknow”,可以理解为未定义或者未知,因此任何与Null值进行比对的二元操作符结果一定为Null,包括Null值本身。而在SQL Server中,Null值的含义转换为Bool类型的结果为False。 SQL Server提供了“IS”操作符与Null值做对比,用于衡量某个值是否为Null。

 

这里还要说一下not in的问题,应尽量避免使用not in,not in结果不准确,性能效率低。可以采用not exists方案替代

select ID from SmartCustomer a where  not exists (select ID from SmartPromoter b where a.ID=b.CustomerID)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM