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);

猜你喜欢

转载自www.cnblogs.com/rensheng/p/12019782.html