常見資料庫分頁SQL語句

2021-04-20 07:10:39 字數 1558 閱讀 4107

sql server 

從資料庫表中的第m條記錄開始取n條記錄,利用top關鍵字:注意如果select語句中既有top,又有order by,則是從排序好的結果集中選擇:

select *

from  ( select top n *  

from  (select top (m + n - 1) * from 表名稱 order by 主鍵 desc) t1 ) t2

order by 主鍵 asc

例如從表sys_option(主鍵為sys_id)中從10條記錄還是檢索20條記錄,語句如下:

select * 

from ( select top 20 *

from (select top 29 * from sys_option order by sys_id desc) t1) t2

order by sys_id asc

select * from (select top 20 * from (select top 29 * from m_info_jg)t1)t2 order by id asc

oralce資料庫

從資料庫表中第m條記錄開始檢索n條記錄

select * 

from (select rownum r,t1.* from 表名稱 t1 where rownum < m + n) t2

where t2.r >= m

例如從表sys_option(主鍵為sys_id)中從10條記錄還是檢索20條記錄,語句如下:

select * 

from (select rownum r,t1.* from sys_option where rownum < 30 ) t2

where t2.r >= 10

minus差分頁 select * from table where rownum<=10 minus select * from table where rownum<=5

rownum偽列select * from (select rownum tid,t.* from table t where rownum<=10) where tid<=10 and tid>=5

notin相反select * from table where id not in(select id from table where rownum<=5) and rownum<=5

前題是id排序的select * from table where id>(select max(id) from table where rownum<=5) and rownum<=5

my sql資料庫

my sql資料庫最簡單,是利用mysql的limit函式,limit [offset,] rows從資料庫表中m條記錄開始檢索n條記錄的語句為:

select * from 表名稱 limit m,n

例如從表sys_option(主鍵為sys_id)中從10條記錄還是檢索20條記錄,語句如下:

select * from sys_option limit 10,20

常見資料庫分頁SQL語句

我們在編寫mis系統和web應用程式等系統時,都涉及到與資料庫的互動,如果資料庫中資料量很大的話,一次檢索所有的記錄,會占用系統很大的資源,因此我們常常採用,需要多少資料就只從資料庫中取多少條記錄,即採用分頁語句。根據自己使用過的內容,把常見資料庫sql server,oracle和my sql的分...

常見資料庫分頁SQL語句

sql server 從資料庫表中的第m條記錄開始取n條記錄,利用top關鍵字 注意假如select語句中既有top,又有order by,則是從排序好的結果集中選擇 select from select top n from select top m n 1 from 表名稱 order by 主...

常見資料庫分頁SQL語句

我們在編寫mis系統和web應用程式等系統時,都涉及到與資料庫的互動,如果資料庫中資料量很大的話,一次檢索所有的記錄,會占用系統很大的資源,因此我們常常採用,需要多少資料就只從資料庫中取多少條記錄,即採用分頁語句。根據自己使用過的內容,把常見資料庫sql server,oracle和my sql的分...