ORACLE 在not in中使用null的问题
2009-12-02 21:46
676 查看
以前还专门小总结过一下ORACLE中关于NULL的一些问题,碰巧今天在看书的过程中又看到了另外一个以前没发现的需要注意的地方,那就是在not in中使用null的问题。
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
DEPTNO
----------
10
//看到使用in的时候即便有null 也是正常的 下面看一下not in
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
DEPTNO
----------
20
30
40
//这里看起来和我们的预期挺符合的哦
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
no rows selected
//怎么回事 为什么加了个null 前面的20、30、40三条数据就不显示出来了
IN和NOT IN本质上都是OR运算,因而计算逻辑OR时处理NULL的方式不同,产生的结果也不同。
下面我们分析一下前面的三条语句
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
这里可以等价于where deptno=10 or deptno=50 or deptno=null,由于是or相连接,那么只要有一个条件为TRUE,整个就喂TRUE了。所以deptno为10的记录显示出来了。
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
这里等价于where not (deptno=10 or deptno=50 or deptno=null),拿deptno=20的记录来举例吧。
not (20=10 or 20=50 or 20=null)
not(false or false or null)
not null
null
(以前只知道在where条件返回false的时候不成立,现在看来返回NULL的时候也不成立呀,下面是做的一个小实验可以证明这个猜想)
#####################
SQL> select * from dept
2 where 1=null;
no rows selected
#####################
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
这里等价于where not (deptno=10 or deptno=50),依然拿deptno=20来举例。
not (20=10 or 20=50 )
not(false or false)
not false
true
注意:FALSE OR NULL=NULL ,而TRUE OR NULL=TRUE。
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
DEPTNO
----------
10
//看到使用in的时候即便有null 也是正常的 下面看一下not in
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
DEPTNO
----------
20
30
40
//这里看起来和我们的预期挺符合的哦
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
no rows selected
//怎么回事 为什么加了个null 前面的20、30、40三条数据就不显示出来了
IN和NOT IN本质上都是OR运算,因而计算逻辑OR时处理NULL的方式不同,产生的结果也不同。
下面我们分析一下前面的三条语句
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
这里可以等价于where deptno=10 or deptno=50 or deptno=null,由于是or相连接,那么只要有一个条件为TRUE,整个就喂TRUE了。所以deptno为10的记录显示出来了。
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
这里等价于where not (deptno=10 or deptno=50 or deptno=null),拿deptno=20的记录来举例吧。
not (20=10 or 20=50 or 20=null)
not(false or false or null)
not null
null
(以前只知道在where条件返回false的时候不成立,现在看来返回NULL的时候也不成立呀,下面是做的一个小实验可以证明这个猜想)
#####################
SQL> select * from dept
2 where 1=null;
no rows selected
#####################
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
这里等价于where not (deptno=10 or deptno=50),依然拿deptno=20来举例。
not (20=10 or 20=50 )
not(false or false)
not false
true
注意:FALSE OR NULL=NULL ,而TRUE OR NULL=TRUE。
相关文章推荐
- ORACLE 在not in中使用null的问题
- Oracle 在not in中使用null的问题
- oracle中not in(null)问题
- Oracle not in子连接查询不到值的问题(not in 不能查询null数据)
- oracle使用相关问题
- 使用JDBC时与Oracle的类型匹配问题
- oracle、sqlserver数据库排序空值null问题解决办法
- oracle使用过程遇到的小问题及解决方法
- 使用PL/SQL Developer连接远程oracle客户端报错问题解决记录
- Oracle中null的使用详解
- 转:Oracle中的rownum不能使用大于>的问题
- oracle 使用IN 不能超过1000问题的解决(oracle中列表中的最大表达式数为 1000)
- 关于oracle中的wmsys.wm_concat中的使用问题
- Oracle Max函数使用中出现的问题
- Oracle查询问题引发临时表使用
- Oracle.DataAccess 驱动使用因为SQL语句缓存报错问题解决办法
- Spring+Mybatis使用Oracle的XMLTable过程中碰到的问题
- hive中使用is null和is not null问题
- Oracle使用sqlplus和rman无法使用退格、上翻键乱码等问题的解决办法
- 解决 SpringMvc 非controller类使用@Autowired注解 service注入为null的问题