ORACLE 在not in中使用null的问题
以前还专门小总结过一下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
相关文档:
查询表emp中所有数据
select emp_id,rownum from emp
第一步,查询结果,rownum待定
emp_id rownum
1 ? 1
2 ? 2
3 ? 3
4&n ......
最简单的是使用
/* Formatted on 2009/12/02 16:01 (Formatter Plus v4.8.8) */
SELECT TO_CHAR (TO_DATE ('2009-12-02', 'YYYY-MM-DD'), 'WW')
from DUAL;
不过to_char()函数在计算一年中第几周是从该年的1月1日开始的。
以下SQL 能得到正确的自然周数.
/* Formatted ......
1、编写目的
使用统一的命名和编码规范,使数据库命名及编码风格标准化,以便于阅读、理解和继承。
2、适用范围
本规范适用于公司范围内所有以ORACLE作为后台数据库的应用系统和项目开发工作。
3、对象命名规范
3.1 数据库和SID
数据库名定义为系统名+模块名
★ 全局数据库名和例程SID 名要求一致
★ 因SID ......
SQL> select sal,comm from emp
2 where sal<2000;
SAL COMM
---------- ----------
800
1600 300
1250 500
1250 1400
1500 0
1100
950
1300
8 rows selected.
SQL> select sal as salary,comm as ......