Oracle、SQL Server、Access資料庫高效果分頁技巧

來源:互聯網
上載者:User

1、SQL Server、Access資料庫

這都微軟的資料庫,都是一家人,基本的操作都是差不多,常採用如下分頁語句:

PAGESIZE:每頁顯示的記錄數

CURRENTPAGE:當前頁號

資料表的名字是: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資料庫

因為Oracle資料庫沒有Top關鍵字,所以這裡就不能夠像微軟的資料據那樣操作,這裡有兩種方法:

(1)、一種是利用相反的。

PAGESIZE:每頁顯示的記錄數

CURRENTPAGE:當前頁號

資料表的名字是: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,這個是Oracle查詢自動返回的序號,一般不顯示,但是可以通過select rownum from [表名]看到,注意,它是從1到當前的記錄總數。

以下是引用片段:
select * from (select rownum tid,components.
* from components where rownum<=100) where tid<=10;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.