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)

 

posted @ 2017-05-08 18:34  小破天  阅读(5196)  评论(0编辑  收藏  举报