[轉載]Oracle的rownum原理和使用

Oracle的rownum原理和使用

原文見 http://tenn.javaeye.com/blog/99339

Oracle中,要按特定條件查詢前N條記錄,用個rownum就搞定了。
select * from emp where rownum <= 5
而且書上也告誡,不能對rownum用">",這也就意味着,如果你想用
select * from emp where rownum > 5
則是失敗的。要知道爲什麼會失敗,則需要了解rownum背後的機制:
1 Oracle executes your query.

2 Oracle fetches the first row and calls it row number 1.

3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.

4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).

5 Go to step 3.

瞭解了原理,就知道rownum>不會成功,因爲在第三步的時候查詢出的行已經被丟棄,第四步查出來的rownum仍然是1,這樣永遠也不會成功。

同樣道理,rownum如果單獨用=,也只有在rownum=1時纔有用。

 

對於rownum來說它是oracle系統順序分配爲從查詢返回的行的編號,返回的第一行分配的是1,第二行是2,依此類推,這個僞字段可以用於限制查詢返回的總行數,而且rownum不能以任何表的名稱作爲前綴。
 舉例說明:
例如表:student(學生)表,表結構爲:
ID       char(6)      --學號
name    VARCHAR2(10)   --姓名
create table student (ID char(6), name VARCHAR2(100));
insert into sale values('200001',‘張一’);
insert into sale values('200002',‘王二’);
insert into sale values('200003',‘李三’);
insert into sale values('200004',‘趙四’);
commit;
(1) rownum 對於等於某值的查詢條件
如果希望找到學生表中第一條學生的信息,可以使用rownum=1作爲條件。但是想找到學生表中第二條學生的信息,使用rownum=2結果查不到數據。因爲rownum都是從1開始,但是1以上的自然數在rownum做等於判斷是時認爲都是false條件,所以無法查到rownum = n(n>1的自然數)。
SQL> select rownum,id,name from student where rownum=1;(可以用在限制返回記錄條數的地方,保證不出錯,如:隱式遊標)
SQL> select rownum,id,name from student where rownum=1;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200001 張一
SQL> select rownum,id,name from student where rownum =2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
(2)rownum對於大於某值的查詢條件
   如果想找到從第二行記錄以後的記錄,當使用rownum>2是查不出記錄的,原因是由於rownum是一個總是從1開始的僞列,Oracle 認爲rownum> n(n>1的自然數)這種條件依舊不成立,所以查不到記錄
SQL> select rownum,id,name from student where rownum >2;
ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
那如何才能找到第二行以後的記錄呀。可以使用以下的子查詢方法來解決。注意子查詢中的rownum必須要有別名,否則還是不會查出記錄來,這是因爲rownum不是某個表的列,如果不起別名的話,無法知道rownum是子查詢的列還是主查詢的列。
SQL>select * from(select rownum no ,id,name from student) where no>2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         4 200004 趙四
SQL> select * from(select rownum,id,name from student)where rownum>2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
(3)rownum對於小於某值的查詢條件
如果想找到第三條記錄以前的記錄,當使用rownum<3是能得到兩條記錄的。顯然rownum對於rownum<n((n>1的自然數)的條件認爲是成立的,所以可以找到記錄。
SQL> select rownum,id,name from student where rownum <3;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
1 200001 張一
        2 200002 王二
綜上幾種情況,可能有時候需要查詢rownum在某區間的數據,那怎麼辦呀從上可以看出rownum對小於某值的查詢條件是人爲true的,rownum對於大於某值的查詢條件直接認爲是false的,但是可以間接的讓它轉爲認爲是true的。那就必須使用子查詢。例如要查詢rownum在第二行到第三行之間的數據,包括第二行和第三行數據,那麼我們只能寫以下語句,先讓它返回小於等於三的記錄行,然後在主查詢中判斷新的rownum的別名列大於等於二的記錄行。但是這樣的操作會在大數據集中影響速度。
SQL> select * from (select rownum no,id,name from student where rownum<=3 ) where no >=2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         2 200002 王二
         3 200003 李三
(4)rownum和排序
Oracle中的rownum的是在取數據的時候產生的序號,所以想對指定排序的數據去指定的rowmun行數據就必須注意了。
SQL> select rownum ,id,name from student order by name;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         2 200002 王二
         1 200001 張一
         4 200004 趙四
可以看出,rownum並不是按照name列來生成的序號。系統是按照記錄插入時的順序給記錄排的號,rowid也是順序分配的。爲了解決這個問題,必須使用子查詢
SQL> select rownum ,id,name from (select * from student order by name);
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200003 李三
         2 200002 王二
         3 200001 張一
         4 200004 趙四
這樣就成了按name排序,並且用rownum標出正確序號(有小到大)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章