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','空');
本文深入探讨了Oracle数据库中NULL值在IN和NOT IN操作符中的特殊行为。解释了为何IN操作符能接受NULL值,而NOT IN则不能,并提供了SQL改造建议,帮助读者理解并正确使用这些操作符。
1178

被折叠的 条评论
为什么被折叠?



