Oracle的rownum原理和使用

 在Oracle中,要按特定條件查詢前N條記錄,用個rownum就搞定了。 oracle

  select * from emp where rownum <= 5 fetch

  並且書上也告誡,不能對rownum用">",這也就意味着,若是你想用 大數據

  select * from emp where rownum > 5 排序

  則是失敗的。要知道爲何會失敗,則須要瞭解rownum背後的機制: get

  1 Oracle executes your query. it

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

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

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

  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對於rownum1的天然數)的條件認爲是成立的,因此能夠找到記錄。

  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標出正確序號(有小到大)

相關文章
相關標籤/搜索