sql语句中使用in、not in 查询时,注意条件范围中的null值处理事项


emp表中的数据

 

 

1. 使用in的时候,忽略为null的,不会查询出comm为null的数据

select * from emp e where e.comm in (300, 500, null);

2. 使用not in的时候,如果 not in后面的选项中没有null,只会查询从comm列不为空的列中过滤,会过滤掉comm为null的数据

select * from emp e where e.comm not in (300, 500);

3. 使用not in 的时候,如果not in后面的选项中有null,不会查询出来任何数据。sql语句本身直接返回false,所以使用not in的时候,要保证in中的条件不会出现null的情况,不然可能会出现意想不到的情况。

select * from emp e where e.comm not in (300, 500, null);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM