亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

sqlServer實(shí)現(xiàn)分頁(yè)查詢(xún)的三種方式

 更新時(shí)間:2023年03月02日 12:03:03   作者:格子衫111  
在SqlServer中分頁(yè)查詢(xún)是經(jīng)常用到的查詢(xún)語(yǔ)句,一個(gè)好的分頁(yè)查詢(xún)語(yǔ)句,不能將代碼省略,下面這篇文章主要給大家介紹了關(guān)于sqlServer實(shí)現(xiàn)分頁(yè)查詢(xún)的三種方式,需要的朋友可以參考下

sqlServer的分頁(yè)查詢(xún)和mysql語(yǔ)句不一樣,有三種實(shí)現(xiàn)方式。分別是:offset /fetch next、利用max(主鍵)、利用row_number關(guān)鍵字

一、offset /fetch next關(guān)鍵字

2012版本及以上才有,SQL server公司升級(jí)后推出的新方法。

公式:

-- 分頁(yè)查詢(xún)公式-offset /fetch next
select * from 表名
order by 主鍵 或 其他索引列 
-- @pageIndex:頁(yè)碼、@pageSize:每頁(yè)記錄數(shù)
offset ((@pageIndex-1)*@pageSize) rows
fetch next @pageSize rows only;

示例:

-- 分頁(yè)查詢(xún)第2頁(yè),每頁(yè)有10條記錄
select * from tb_user
order by uid
offset 10 rows
fetch next 10 rows only ;

說(shuō)明:

offset 10 rows ,將前10條記錄舍去,fetch next 10 rows only ,向后再讀取10條數(shù)據(jù)。

二、利用max(主鍵)

公式:

-- 分頁(yè)查詢(xún)公式-利用max(主鍵)
select top @pageSize * 
from 表名 
where 主鍵>=
(select max(主鍵) 
	from (
		select top ((@pageIndex-1)*@pageSize+1) 主鍵
		from 表名  
		order by  主鍵 asc) temp_max_ids) 
order by 主鍵;

示例:

-- 分頁(yè)查詢(xún)第2頁(yè),每頁(yè)有10條記錄
select top 10 * 
from tb_user 
-- 3、再重新在這個(gè)表查詢(xún)前10條,條件: id>=max(id)
where uid>=
-- 2、利用max(id)得到前11條記錄中最大的id
(select max(uid) 
	from (
		-- 1、先top前11條行記錄
		select top 11 uid
		from tb_user 
		order by  uid asc) temp_max_ids) 
order by uid;

說(shuō)明:

先top前11條行記錄,然后利用max(id)得到最大的id,之后再重新在這個(gè)表查詢(xún)前10條,不過(guò)要加上條件,where id>=max(id)。

中心思想:其實(shí)就是先得到該頁(yè)的初始id,PS:別忘了加上排序哦

三、利用row_number關(guān)鍵字

這種方式也是比較常用的,直接利用row_number() over(order by id)函數(shù)計(jì)算出行數(shù),選定相應(yīng)行數(shù)返回即可,不過(guò)該關(guān)鍵字只有在SQL server 2005版本以上才有。

公式:

-- 分頁(yè)查詢(xún)公式-row_number()
select top @pageSize * 
from (
	-- rownumber是別名,可按自己習(xí)慣取
	select row_number() over(order by 主鍵 asc) as rownumber,* 
	from 表名) temp_row
where rownumber>((@pageIndex-1)*@pageSize);

示例:

-- 分頁(yè)查詢(xún)第2頁(yè),每頁(yè)有10條記錄
select top 10 * 
from (
	-- 子查詢(xún),多加一個(gè)rownumber列返回
	select row_number() over(order by uid asc) as rownumber,* 
	from tb_user) temp_row
	--限制起始行標(biāo)
where rownumber>10;

說(shuō)明:

利用row_number函數(shù)給每行記錄標(biāo)了一個(gè)序號(hào),相當(dāng)于在原表中多加了1列返回。

上述示例,是以序號(hào)11為起始行,查詢(xún)前10條記錄,即為第2頁(yè)數(shù)據(jù)。

優(yōu)化:

可以看到,子查詢(xún)查詢(xún)了全表數(shù)據(jù),如果數(shù)據(jù)量大,效率是比較低的。

下面是優(yōu)化后的SQL,

公式:

-- 分頁(yè)查詢(xún)公式-row_number()-優(yōu)化版本
select * 
from (
	-- rownumber是別名,可按自己習(xí)慣取
	select top (@pageIndex*@pageSize) row_number() over(order by 主鍵 asc) 
	as rownumber,* 
	from 表名) temp_row
where rownumber>((@pageIndex-1)*@pageSize);

示例:

-- 分頁(yè)查詢(xún)第2頁(yè),每頁(yè)有10條記錄
select * 
from (
	-- 子查詢(xún),限制了返回前20條數(shù)據(jù)
	select top 20 row_number() over(order by uid asc) as rownumber,* 
	from tb_user) temp_row
	--限制起始行標(biāo)
where rownumber>10;

說(shuō)明:

這里,子查詢(xún)僅查詢(xún)到當(dāng)前頁(yè)的最后一行,沒(méi)有進(jìn)行全表查詢(xún),所以效率上要快一點(diǎn)。在外層限制起始行標(biāo),是沒(méi)變的,但是卻在內(nèi)層控制了結(jié)尾行標(biāo)。

上述示例,是以序號(hào)11為起始行,查詢(xún)20以?xún)?nèi)的記錄,即為第2頁(yè)數(shù)據(jù)。

總結(jié)

更多介紹,可查看我的另外篇文章:SQL Server中row_number函數(shù)用法介紹

到此這篇關(guān)于sqlServer實(shí)現(xiàn)分頁(yè)查詢(xún)的三種方式的文章就介紹到這了,更多相關(guān)sqlServer分頁(yè)查詢(xún)實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論