Oracle not in 後面有null 查詢不到記錄!

1.概述:

Oracle中 null不能參與比較運算符,即與任何數據比較結果都爲null。

in 後面可以有null ,not in後面不能有null

2. 爲什麼in 後面可以有null

select * from table1 as a where a.id in ( '1', null );

等同於

select * from table1 as a where a.id='1' or a.id=null;

當id=1 時 a.id=1爲true 返回數據。

當id=null時 a.id='1'爲false ,a.id=null  爲null=null 結果爲null 所以並不返回數據。

3. 爲什麼not in後面不可以有null

select * from table1 as a where a.id not in ( '1', null );

等同於

select * from talbe1 as a where a.id<>'1' and a.id<>null;

a.id<>null 的結果爲null ,所以此sql不會返回數據。

4. 改造sql

select * from table1 as a where nvl(a.id,'空' ) not in ('1','空');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章