Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1702814
  • 博文数量: 136
  • 博客积分: 10021
  • 博客等级: 上将
  • 技术积分: 3261
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-22 11:26
文章分类

全部博文(136)

文章存档

2010年(1)

2009年(26)

2008年(109)

我的朋友

分类: Oracle

2009-04-09 16:19:51

我们知道,null是“未知的数据”。但在排序的时候,Oracle会认为它“无限大”,放在排序列的“最大”位置。如果不想这样,你需要使用nvl函数。下面是一些小测试:
 
1. 环境
 
SQL> select * from v$version;
 
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
 
SQL> create table t1 as select rownum a, 'yct'||rownum b, add_months(sysdate, rownum) c from dual connect by rownum < 11;
 
Table created
 
SQL> desc t1
Name Type         Nullable Default Comments 
---- ------------ -------- ------- -------- 
A    NUMBER       Y                         
B    VARCHAR2(43) Y                         
C    DATE         Y  
 
SQL> update t1 set a = null, b = null, c = null where rownum < 4;
 
已更新3行。
 
SQL> commit;
 
提交完成。
 
SQL> select * from t1;
 
         A B                                           C
---------- ------------------------------------------- --------------
 
 
 
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
        10 yct10                                       09-2月 -10
 
已选择10行。
 
2. 对于null数据,按照“最大”值排列,总是放在排序列的最大位置出现。
 
SQL> select * from t1 order by a;
 
         A B                                           C
---------- ------------------------------------------- --------------
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
        10 yct10                                       09-2月 -10
 
 
 
已选择10行。
 
SQL> select * from t1 order by a desc;
 
         A B                                           C
---------- ------------------------------------------- -------------
 
 
 
        10 yct10                                       09-2月 -10
         9 yct9                                        09-1月 -10
         8 yct8                                        09-12月-09
         7 yct7                                        09-11月-09
         6 yct6                                        09-10月-09
         5 yct5                                        09-9月 -09
         4 yct4                                        09-8月 -09
 
已选择10行。
 
SQL> select * from t1 order by b;
 
         A B                                           C
---------- ------------------------------------------- --------------
        10 yct10                                       09-2月 -10
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
 
 
 
已选择10行。
 
SQL> select * from t1 order by b desc;
         A B                                           C
---------- ------------------------------------------- ------------
 
 
 
         9 yct9                                        09-1月 -10
         8 yct8                                        09-12月-09
         7 yct7                                        09-11月-09
         6 yct6                                        09-10月-09
         5 yct5                                        09-9月 -09
         4 yct4                                        09-8月 -09
        10 yct10                                       09-2月 -10
 
已选择10行。
 
SQL> select * from t1 order by c;
 
         A B                                           C
---------- ------------------------------------------- --------------
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
        10 yct10                                       09-2月 -10
 
 
 
已选择10行。
 
SQL> select * from t1 order by c desc;
 
         A B                                           C
---------- ------------------------------------------- -----------
 
 
 
        10 yct10                                       09-2月 -10
         9 yct9                                        09-1月 -10
         8 yct8                                        09-12月-09
         7 yct7                                        09-11月-09
         6 yct6                                        09-10月-09
         5 yct5                                        09-9月 -09
         4 yct4                                        09-8月 -09
 
已选择10行。
 
3. 其实很多时候,要把null数据看作是“最小”值来排序,这就用到nvl函数:
 
SQL> select * from t1 order by nvl(a,0);
 
         A B                                           C
---------- ------------------------------------------- --------------
 
 
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
        10 yct10                                       09-2月 -10
 
已选择10行。
 
SQL> select * from t1 order by nvl(b,'a');
 
         A B                                           C
---------- ------------------------------------------- --------------
        10 yct10                                       09-2月 -10
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
 
已选择10行。
 
SQL> select * from t1 order by nvl(c,to_date('0001-01-01', 'yyyy-mm-dd'));
 
         A B                                           C
---------- ------------------------------------------- --------------
         4 yct4                                        09-8月 -09
         5 yct5                                        09-9月 -09
         6 yct6                                        09-10月-09
         7 yct7                                        09-11月-09
         8 yct8                                        09-12月-09
         9 yct9                                        09-1月 -10
        10 yct10                                       09-2月 -10
 
已选择10行。
 
阅读(5540) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-11-25 22:01:48

排序时,oracle默认将null作为最大的值,升序时,null值排到尾部;降序时,null排到前面。可以将 null 值设定到指定的排序位置(前面或者后面)。例如: order by c desc nulls last;或者 order by c desc nulls first;