注意SQL中使用not in 的陷阱

在MSSQL 2008中編寫SQL語句時使用到了not in 遇到了一個陷阱

select top 10 * from A where AID NOT IN (select BPID From B where BID='1' )

在返回的BPID中存在null值的時候,即使AID滿足NOT IN(BPID_Values)也不會返回數據,改成如下語句就可以了

select top 10 * from A where AID NOT IN (select BPID From B where BID='1' and BID not null )

猜你喜欢

转载自blog.csdn.net/qq_24886681/article/details/82691229