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)