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','空');

 

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