[Oracle] 大话Oracle中的NULL

null与in:null值不会包含在in的结果集中。


A105024@O02DMS1>select * from test;

        ID NAME
---------- --------------------
         1 A
             B
         3 C

A105024@O02DMS1>select * from test where id in (1);

        ID NAME
---------- --------------------
         1 A

A105024@O02DMS1>select * from test where id not in(1);

        ID NAME
---------- --------------------
         3 C

从上面的例子我们可以看出,不管是in还是not in,null值都不在结果集中,除非显示的声明:
A105024@O02DMS1>select * from test where id is null;

        ID NAME
---------- --------------------
           B

-->null与“加减乘除等于”的关系:null不能用来进行等于比较;与任何东西的加减乘除的结果都是null。
A105024@O02DMS1>select * from test where id = null;

no rows selected

A105024@O02DMS1>select id+1 from test where id is null;

      ID+1
----------

null与集合运算的关系:集合运算中将所有的null作为相等的值来对待。


A105024@O02DMS1>select null from dual union select null from dual;

N
-

A105024@O02DMS1>select null from dual minus select null from dual;

no rows selected

-->null与Group by 的关系:group by把null作为一个值来对待。
A105024@O02DMS1>select id,count(*) from test group by id;

        ID   COUNT(*)
---------- ----------
         1          1
                     1
         3          1

-null与Order by的关系:order by 默认排序规则是将空值放在最后(不管是升序还是降序),除非在order by后面加上nulls first


A105024@O02DMS1>select * from test order by id;

        ID NAME
---------- --------------------
         1 A
         3 C
            B

A105024@O02DMS1>select * from test order by id desc;

        ID NAME
---------- --------------------
            B
         3 C
         1 A

A105024@O02DMS1>select * from test order by id nulls first;

        ID NAME
---------- --------------------
            B
         1 A

         3 C


null与聚合函数的关系:聚合函数如sum,avg,min,max将忽略null值,一个例外是count,count(*)包含null值,而count(列名)不包含null值。


A105024@O02DMS1>select count(*) from test;

  COUNT(*)
----------
         3

A105024@O02DMS1>select count(id) from test;

 COUNT(ID)
----------
         2

你可能感兴趣的:(oracle,null)