常用3种数据库的Sql分页
生活随笔
收集整理的這篇文章主要介紹了
常用3种数据库的Sql分页
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在程序的開發(fā)過程中,處理分頁是大家接觸比較頻繁的事件,因?yàn)楝F(xiàn)在軟件基本上都是與數(shù)據(jù)庫進(jìn)行掛釣的。但效率又是我們所追求的,如果是像原來那樣把所有滿足條件的記錄全部都選擇出來,再去進(jìn)行分頁處理,那么就會(huì)多多的浪費(fèi)掉許多的系統(tǒng)處理時(shí)間。為了能夠把效率提高,所以現(xiàn)在我們就只選擇我們需要的數(shù)據(jù),減少數(shù)據(jù)庫的處理時(shí)間,以下就是常用SQL分頁處理:?
1、SQL Server、Access數(shù)據(jù)庫?
這都微軟的數(shù)據(jù)庫,都是一家人,基本的操作都是差不多,常采用如下分頁語句:?
PAGESIZE:每頁顯示的記錄數(shù)?
CURRENTPAGE:當(dāng)前頁號(hào)?
數(shù)據(jù)表的名字是:components?
索引主鍵字是:id?
select top PAGESIZE * from components where id not in
(select top (PAGESIZE*(CURRENTPAGE-1))?
id from components order by id)order by id
如下列:?
select top 10 * from components where id not in
(select top 10*10 id from components order by id)
order by id
從101條記錄開始選擇,只選擇前面的10條記錄
2、Oracle數(shù)據(jù)庫?
因?yàn)镺racle數(shù)據(jù)庫沒有Top關(guān)鍵字,所以這里就不能夠像微軟的數(shù)據(jù)據(jù)那樣操作,這里有兩種方法:?
(1)、一種是利用相反的。?
PAGESIZE:每頁顯示的記錄數(shù)?
CURRENTPAGE:當(dāng)前頁號(hào)?
數(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條。?
(2)、使用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 components?
where rownum<=5;.
(3)、一種是利用Oracle的rownum,這個(gè)是Oracle查詢自動(dòng)返回的序號(hào),一般不顯示,但是可以通過select rownum from [表名]看到,注意,它是從1到當(dāng)前的記錄總數(shù)。?
select * from (select rownum tid,components.
* from components where rownum<=100) where tid<=10;
1、SQL Server、Access數(shù)據(jù)庫?
這都微軟的數(shù)據(jù)庫,都是一家人,基本的操作都是差不多,常采用如下分頁語句:?
PAGESIZE:每頁顯示的記錄數(shù)?
CURRENTPAGE:當(dāng)前頁號(hào)?
數(shù)據(jù)表的名字是:components?
索引主鍵字是:id?
select top PAGESIZE * from components where id not in
(select top (PAGESIZE*(CURRENTPAGE-1))?
id from components order by id)order by id
如下列:?
select top 10 * from components where id not in
(select top 10*10 id from components order by id)
order by id
從101條記錄開始選擇,只選擇前面的10條記錄
2、Oracle數(shù)據(jù)庫?
因?yàn)镺racle數(shù)據(jù)庫沒有Top關(guān)鍵字,所以這里就不能夠像微軟的數(shù)據(jù)據(jù)那樣操作,這里有兩種方法:?
(1)、一種是利用相反的。?
PAGESIZE:每頁顯示的記錄數(shù)?
CURRENTPAGE:當(dāng)前頁號(hào)?
數(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條。?
(2)、使用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 components?
where rownum<=5;.
(3)、一種是利用Oracle的rownum,這個(gè)是Oracle查詢自動(dòng)返回的序號(hào),一般不顯示,但是可以通過select rownum from [表名]看到,注意,它是從1到當(dāng)前的記錄總數(shù)。?
select * from (select rownum tid,components.
* from components where rownum<=100) where tid<=10;
總結(jié)
以上是生活随笔為你收集整理的常用3种数据库的Sql分页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个程序员的感慨的《虚拟光驱》
- 下一篇: 什么叫企业级即时通讯软件