新聞中心
論述Oracle分頁查詢的幾種方式
oracle,
創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、博羅網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為博羅等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
sql
server
和mysql的分頁sql語句如下:oracle:方法一:select
*
from(select
a.*,
rownum
rn
from
(select
*
from
table_name)
a
where
rownum
=
40)where
rn
=
21;方法二:select
*
from(select
a.*,
rownum
rn
from
(select
*
from
table_name)
a)where
rn
between
21
and
40
公認(rèn)第二種方法效率沒有第一種高。原因是第二種要把子查詢執(zhí)行完,而第一種方法子查詢執(zhí)行到rownum=40后就結(jié)束了。mysql:
select
*
from
table_name
limit
10,
20
表示從第11條數(shù)據(jù)開始取20條數(shù)據(jù)返回,limit后的2個參數(shù)含義為:起點(diǎn)和步長,即從那條數(shù)據(jù)開始,取多少條數(shù)據(jù),再如取前20條數(shù)據(jù):select
*
from
table_name
limit
0,
20
sql
server2000:
select
top
@pagesize
*
from
table_name
where
id
not
in
(select
top
@pagesize*(@page-1)
id
from
table_name
order
by
id)
order
by
id
Oracle之分頁
在Oracle中有一個方法rownum用來查詢第一行到第n行的內(nèi)容,但沒有一個合適的方法若查詢第x行到第y行的內(nèi)容,而在實際應(yīng)用中卻經(jīng)常需要查詢第x行到第y行的內(nèi)容,這時我們就需要使用rownum和子表查詢等內(nèi)容來進(jìn)行查詢,因為這一塊內(nèi)容屬于Oracle總的常用部分所以專門在此介紹。
在Oralce中有一個偽列rownum,其在創(chuàng)建表的時候就存在了卻不顯示,若要使用這個列可以直接調(diào)用即可,也可以對這個列添加別名來調(diào)用。
rownum只能用于顯示小于某行的數(shù)據(jù)即第一行開始到你要查詢到的那一行為止的數(shù)據(jù)。
在Oracle把查詢第幾行到第幾行的操作稱為分頁,其具體操作是通過子查詢等操作完成。
select 列名 from (select 表名.*,rownum rn from 表名)表名 ?where rn操作;
思考如下:
1.選擇所有內(nèi)容
select * from emp;
2.顯示rownum
select e.*,rownum rn from(select * from emp)e;
這一步可以精簡為下面形式,但某些情況只能用上面那種
select emp.*,rownum rn from emp;
3.查詢
select * from(select e.*,rownum rn from (select * from emp)e);
4.其他變化
在某些時候我們需要先對表的內(nèi)容進(jìn)行排序,隨后查詢第x行到第y行的內(nèi)容,這個時候有一個需要注意的點(diǎn)是rownum是在表產(chǎn)生的時候產(chǎn)生的偽列,所以使用排序會連著rownum的值進(jìn)行排序,從而達(dá)不到想要的效果。
為了解決上述這個問題,我們需要使用子表查詢即先排好序,再在新表之中顯示rownum來規(guī)避這個問題。
考慮到排序的問題,所以在上方第二步的時候使用第一種方法即select e.*,rownum rn from(select * from emp)e;,在內(nèi)表select * from emp中進(jìn)行排序可以完成在亂序中找到第x行到第y行的效果。
oracle 數(shù)據(jù)庫中 如何分頁???????
下面兩種用可以用rownum的變通方式實現(xiàn)分頁:
select
*
from
(select
rownum
row_num,month,sell
from
(select
month,sell
from
sale
group
by
month,sell)
)
where
row_num
between
5
and
9;【網(wǎng)友評論】
select
dmp.row_num,dmp.requirement_id
from
(select
rownum
as
row_num,
requirement_id
from
(select
requirement_id
from
requirement
order
by
requirement_id
desc)
)
dmp
where
row_num
between
10
and
20;【網(wǎng)友評論】
oracle怎么實現(xiàn)分頁
因為Oracle數(shù)據(jù)庫沒有Top關(guān)鍵字,所以這里就不能夠像微軟的數(shù)據(jù)據(jù)那樣操作,這里有兩種方法:
一種是利用相反的。
PAGESIZE:每頁顯示的記錄數(shù)
CURRENTPAGE:當(dāng)前頁號
數(shù)據(jù)表的名字是:components
索引主鍵字是:id
select * from components where id not in(select id from components where rownum=(PAGESIZE*(CURRENTPAGE-1))) and rownum=PAGESIZE order by id;
如下例:
select * from components where id not in(select id from components where rownum=100) and rownum=10 order by id;
從101到記錄開始選擇,選擇前面10條。
使用minus,即中文的意思就是減去,呵呵,這語句非常的有意思,也非常好記
select * from components where rownum=(PAGESIZE*(CURRENTPAGE-1)) minus select * from components where rownum=(PAGESIZE*(CURRENTPAGE-2));
如例:select * from components where rownum=10 minus select * from
一種是利用Oracle的rownum,這個是Oracle查詢自動返回的序號,一般不顯示,但是可以通過select rownum from [表名],可以看到,是從1到當(dāng)前的記錄總數(shù)。
select * from (select rownum tid,components.* from components where rownum=100) where tid=10;
分享文章:oracle是怎么分頁,oracle sql 分頁
分享地址:http://www.dlmjj.cn/article/hdesoe.html