我的java天地

          oracle中的rownum偽列的使用

          下面是收集的兩篇關于 Oracel 用ROWNUM實現分頁的文章:

          推薦直接看第二篇,是Oracle的Tom寫的,最權威
          第一篇做個參考

          第一篇:

          對于rownum來說它是oracle系統順序分配為從查詢返回的行的編號,返回的第一行分配的是1,第二行是2,依此類推,這個偽字段可以用于限制查詢返回的總行數,而且rownum不能以任何表的名稱作為前綴

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


          筆者在工作中有一上百萬條記錄的表,在jsp頁面中需對該表進行分頁顯示, 便考慮用rownum來作,下面是具體方法(每頁
          顯示20條):

          select * from tabname where rownum<20 order by name

          但卻發現oracle卻不能按自己的意愿來執行,而是先隨便
          取20條記錄,然后再 order by,后經咨詢oracle,說rownum確實就這樣,想用的話,只能用子查詢 來實現先排序,后rownum,方法如下:

          select * from (select * from tabname order by name) where rownum<20,但這樣一來,效率會較低很多。
          后經筆者試驗,只需在order by 的字段上加主鍵或索引即可讓oracle先按 該字段排序,然后再rownum;方法不變:
          ?? “select * from tabname where rownum<20 order by name"

          取得某列中第N大的行

          select column_name from
          (select table_name.*,dense_rank() over (order by column desc) rank from table_name)
          where rank = &N;

          假如要返回前5條記錄:
          select * from tablename where rownum<6;(或是rownum <= 5 或是rownum != 6)

          假如要返回第5-9條記錄:

          select * from tablename
          where …
          and rownum<10
          minus
          select * from tablename
          where …
          and rownum<5
          order by name

          選出結果后用name排序顯示結果。(先選再排序)

          注意:只能用以上符號(<、<=、!=)。

          select * from tablename where rownum != 10;返回的是前9條記錄。
          不能用:>,>=,=,Between...and。由于rownum是一個總是從1開始的偽列,Oracle 認為這種條件 不成立,查不到記錄.

          另外,這個方法更快:

          select * from (
          select rownum r,a from yourtable
          where rownum <= 20
          order by name )
          where r > 10
          這樣取出第11-20條記錄!(先選再排序再選)

          要先排序再選則須用select嵌套:內層排序外層選。
          rownum是隨著結果集生成的,一旦生成,就不會變化了;同時,生成的結果是依次遞加的,沒有1就永遠不會有2!
          rownum 是在 查詢集合產生的過程中產生的偽列,并且如果where條件中存在 rownum 條件的話,則:

          1: 假如 判定條件是常量,則:
          只能 rownum = 1, <= 大于1 的自然數, = 大于1 的數是沒有結果的, 大于一個數也是沒有結果的
          即 當出現一個 rownum 不滿足條件的時候則 查詢結束   this is stop key!

          2: 當判定值不是常量的時候
          若條件是 = var , 則只有當 var 為1 的時候才滿足條件,這個時候不存在 stop key ,必須進行 full scan ,對每個滿足其他where條件的數據進行判定
          選出一行后才能去選rownum=2的行……

          第二篇:

          今天找到了Orcale對分頁的講述:
          http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html

          下面是可以正常工作的分頁SQL

          其中A是最內層查詢結果的別名

          下面是非常關鍵的一節,講的十分清楚,推薦各位仔細看看,對了解數據庫查詢如何工作有幫助:

          How ROWNUM Works

          ROWNUM is a pseudocolumn (not a real column) that is available in a query. ROWNUM will be assigned the numbers 1, 2, 3, 4, ... N , where N is the number of rows in the set ROWNUM is used with. A ROWNUM value is not assignedpermanently to a row (this is a common misconception). A row in a table does not have a number; you cannot ask for row 5 from a table—there is no such thing.

          !ROWNUM是在什么時候被賦予每條記錄的!
          Also confusing to many people is when a ROWNUM value is actually assigned. A ROWNUM value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation. Also, a ROWNUM value is incremented only after it is assigned, which is why the following query will never return a row:

          select * 
            from t 
           where ROWNUM > 1;

          Because ROWNUM > 1 is not true for the first row, ROWNUM does not advance to 2. Hence, no ROWNUM value ever gets to be greater than 1. Consider a query with this structure:

          select ..., ROWNUM
            from t
           where <where clause>
           group by <columns>
          having <having clause>
           order by <columns>;

          Think of it as being processed in this order:

          1. The FROM/WHERE clause goes first.
          2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
          3. SELECT is applied.
          4. GROUP BY is applied.
          5. HAVING is applied.
          6.
          ORDER BY is applied.

          That is why a query in the following form is almost certainly an error:

          select * 
            from emp 
           where ROWNUM <= 5 
           order by sal desc;

          The intention was most likely to get the five highest-paid people—a top- N query. What the query will return is five random records (the first five the query happens to hit), sorted by salary. The procedural pseudocode for this query is as follows:

          				
          						ROWNUM = 1
          for x in 
          ( select * from emp )
          loop
              exit when NOT(ROWNUM <= 5)
              OUTPUT record to temp
              ROWNUM = ROWNUM+1
          end loop
          SORT TEMP
          				
          		

          It gets the first five records and then sorts them. A query with WHERE ROWNUM = 5 or WHERE ROWNUM > 5 doesn't make sense. This is because a ROWNUM value is assigned to a row during the predicate evaluation and gets incremented only after a row passes the WHERE clause.

          Here is the correct version of this query:

          select *
            from  
          ( select * 
              from emp 
             order by sal desc ) 
           where ROWNUM <= 5;

          This version will sort EMP by salary descending and then return the first five records it encounters (the top-five records). As you'll see in the top- N discussion coming up shortly, Oracle Database doesn't really sort the entire result set—it is smarter than that—but conceptually that is what takes place.

          第三篇:自已的例子
          select * from(select rownum r,ename,sal from(select ename,sal from scott.emp order by sal desc) a where r<7) b where r>3;

          select * from(select deptno,sal,row_num() over (partition by deptno order by sal desc) as t from scott.emp) where t<7and t>3

          posted on 2009-03-19 22:02 tobyxiong 閱讀(1013) 評論(0)  編輯  收藏 所屬分類: DATABASES

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 张掖市| 清丰县| 清河县| 浦江县| 商南县| 桂东县| 鄄城县| 黔西县| 同德县| 揭阳市| 长沙县| 丰县| 雅安市| 崇左市| 万源市| 南康市| 张掖市| 禹城市| 武川县| 成安县| 乳山市| 噶尔县| 潞城市| 芜湖市| 砚山县| 民丰县| 朔州市| 苏尼特右旗| 大新县| 中山市| 凭祥市| 习水县| 西昌市| 天镇县| 五原县| 略阳县| 利川市| 云梦县| 利辛县| 山丹县| 北碚区|