oracle中的rownum偽列的使用
下面是收集的兩篇關(guān)于 Oracel 用ROWNUM實(shí)現(xiàn)分頁的文章:
推薦直接看第二篇,是Oracle的Tom寫的,最權(quán)威
第一篇做個(gè)參考
第一篇:
對于rownum來說它是oracle系統(tǒng)順序分配為從查詢返回的行的編號,返回的第一行分配的是1,第二行是2,依此類推,這個(gè)偽字段可以用于限制查詢返回的總行數(shù),而且rownum不能以任何表的名稱作為前綴。
(1) rownum 對于等于某值的查詢條件 (具體解釋見第二篇)
如果希望找到學(xué)生表中第一條學(xué)生的信息,可以使用rownum=1作為條件。但是想找到學(xué)生表中第二條學(xué)生的信息,使用rownum=2結(jié)果查不到數(shù)據(jù)。因?yàn)閞ownum都是從1開始,但是1以上的自然數(shù)在rownum做等于判斷是時(shí)認(rèn)為都是false條件,所以無法查到rownum = n(n>1的自然數(shù))。
SQL> select rownum,id,name from student where rownum=1;
(可以用在限制返回記錄條數(shù)的地方,保證不出錯(cuò),如:隱式游標(biāo))
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對于大于某值的查詢條件(具體解釋見第二篇)
?? 如果想找到從第二行記錄以后的記錄,當(dāng)使用rownum>2是查不出記錄的,原因是由于rownum是一個(gè)總是從1開始的偽列,Oracle 認(rèn)為rownum> n(n>1的自然數(shù))這種條件依舊不成立,所以查不到記錄
SQL> select rownum,id,name from student where rownum >2;
ROWNUM ID???? NAME
那如何才能找到第二行以后的記錄呀。可以使用以下的子查詢方法來解決。注意子查詢中的rownum必須要有別名,否則還是不會查出記錄來,這是因?yàn)閞ownum不是某個(gè)表的列,如果不起別名的話,無法知道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對于小于某值的查詢條件
如果想找到第三條記錄以前的記錄,當(dāng)使用rownum<3是能得到兩條記錄的。顯然rownum對于rownum<n((n>1的自然數(shù))的條件認(rèn)為是成立的,所以可以找到記錄。
SQL> select rownum,id,name from student where rownum <3;
??? ROWNUM ID???? NAME
---------- ------ ---------------------------------------------------
??????? 1 200001 張一
??????? 2 200002 王二
綜上幾種情況,可能有時(shí)候需要查詢r(jià)ownum在某區(qū)間的數(shù)據(jù),那怎么辦呀從上可以看出rownum對小于某值的查詢條件是人為true的,rownum對于大于某值的查詢條件直接認(rèn)為是false的,但是可以間接的讓它轉(zhuǎn)為認(rèn)為是true的。那就必須使用子查詢。例如要查詢r(jià)ownum在第二行到第三行之間的數(shù)據(jù),包括第二行和第三行數(shù)據(jù),那么我們只能寫以下語句,先讓它返回小于等于三的記錄行,然后在主查詢中判斷新的rownum的別名列大于等于二的記錄行。但是這樣的操作會在大數(shù)據(jù)集中影響速度。
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的是在取數(shù)據(jù)的時(shí)候產(chǎn)生的序號,所以想對指定排序的數(shù)據(jù)去指定的rowmun行數(shù)據(jù)就必須注意了。
SQL> select rownum ,id,name from student order by name;
??? ROWNUM ID???? NAME
---------- ------ ---------------------------------------------------
???????? 3 200003 李三
???????? 2 200002 王二
???????? 1 200001 張一
???????? 4 200004 趙四
可以看出,rownum并不是按照name列來生成的序號。系統(tǒng)是按照記錄插入時(shí)的順序給記錄排的號,rowid也是順序分配的。為了解決這個(gè)問題,必須使用子查詢
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標(biāo)出正確序號(有小到大)
筆者在工作中有一上百萬條記錄的表,在jsp頁面中需對該表進(jìn)行分頁顯示, 便考慮用rownum來作,下面是具體方法(每頁
顯示20條):
select * from tabname where rownum<20 order by name
但卻發(fā)現(xiàn)oracle卻不能按自己的意愿來執(zhí)行,而是先隨便
取20條記錄,然后再 order by,后經(jīng)咨詢oracle,說rownum確實(shí)就這樣,想用的話,只能用子查詢 來實(shí)現(xiàn)先排序,后rownum,方法如下:
select * from (select * from tabname order by name) where rownum<20,但這樣一來,效率會較低很多。
后經(jīng)筆者試驗(yàn),只需在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
選出結(jié)果后用name排序顯示結(jié)果。(先選再排序)
注意:只能用以上符號(<、<=、!=)。
select * from tablename where rownum != 10;返回的是前9條記錄。
不能用:>,>=,=,Between...and。由于rownum是一個(gè)總是從1開始的偽列,Oracle 認(rèn)為這種條件 不成立,查不到記錄.
另外,這個(gè)方法更快:
select * from (
select rownum r,a from yourtable
where rownum <= 20
order by name )
where r > 10
這樣取出第11-20條記錄!(先選再排序再選)
要先排序再選則須用select嵌套:內(nèi)層排序外層選。
rownum是隨著結(jié)果集生成的,一旦生成,就不會變化了;同時(shí),生成的結(jié)果是依次遞加的,沒有1就永遠(yuǎn)不會有2!
rownum 是在 查詢集合產(chǎn)生的過程中產(chǎn)生的偽列,并且如果where條件中存在 rownum 條件的話,則:
1: 假如 判定條件是常量,則:
只能 rownum = 1, <= 大于1 的自然數(shù), = 大于1 的數(shù)是沒有結(jié)果的, 大于一個(gè)數(shù)也是沒有結(jié)果的
即 當(dāng)出現(xiàn)一個(gè) rownum 不滿足條件的時(shí)候則 查詢結(jié)束 this is stop key!
2: 當(dāng)判定值不是常量的時(shí)候
若條件是 = var , 則只有當(dāng) var 為1 的時(shí)候才滿足條件,這個(gè)時(shí)候不存在 stop key ,必須進(jìn)行 full scan ,對每個(gè)滿足其他where條件的數(shù)據(jù)進(jìn)行判定
選出一行后才能去選rownum=2的行……
第二篇:
今天找到了Orcale對分頁的講述:
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
下面是可以正常工作的分頁SQL

其中A是最內(nèi)層查詢結(jié)果的別名
下面是非常關(guān)鍵的一節(jié),講的十分清楚,推薦各位仔細(xì)看看,對了解數(shù)據(jù)庫查詢?nèi)绾喂ぷ饔袔椭?br />
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是在什么時(shí)候被賦予每條記錄的!
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