Sql Server 分頁查詢

2022-04-03 23:16:19 字數 834 閱讀 6426

sql server 中通過sql語句實現分頁查詢

方案一:利用not in和select top分頁

select top 頁大小 * from 表名

where id not in

(select top 頁大小*(頁數-1) id from 表名 order by id

)order by id

方案二:利用max和select top分頁

select top 頁大小 * from 表名

where id >

(select max(id) from (select top 頁大小*(頁數-1) id from 表名 order by id) a

)order by id

方案三:利用row_number()函式

sql server 2000 不支援

select * from

(select *,row_number() over(order by id) as row from 表名

)as r where r.row>頁大小*(頁數-1) and r.row<=頁大小*頁數

row_number() 語法:

row_number() over (partition column1 order by column2)

根據列column1進行分組,在分組內部根據列column2公升序排列

3種分頁方式,分別是max方案,top方案,row方案

效率

第1:row

第2:max

第3:top

SQL Server 分頁查詢

ps,此文是純個人筆記 公司裡乙個專案裡用到了一種資料庫分頁查詢的方式 1 定義乙個臨時的table 這個table有乙個自增的之間id,和要查的資料表的主鍵id 2 再一次查詢,用id在分頁數段來and 一下結果 具體操作如下 定義個臨時表 temptable declare temptable ...

SQL SERVER 分頁查詢

方式一 row number select top 頁大小 from select row number over order by id as rownumber,from table1 as a where rownumber 頁大小 當前頁 1 註解 首先利用row number 為table...

sql server實現分頁查詢

資料庫分頁查詢 一 mysql 資料庫分頁查詢 mysql資料庫實現分頁比較簡單,提供了limit函式。一般只需要直接寫到sql語句後面就行了。limit子句可以用來限制由select語句返回過來的資料數量,它有乙個或兩個引數,如果給出兩個引數,第乙個引數指定返回的第一行在所有資料中的位置,從0開始...