SQL分頁語句三方案

2022-02-02 18:22:45 字數 1080 閱讀 9003

方法一:

select top 頁大小 *

from table1

where id not in

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

)order by id

方法二:

select top 頁大小 *

from table1

where id >

(select isnull(max(id),0)

from

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

) a)

order by id

方法二倒序:

select top 頁大小 * 

from table1

where id <=

( select isnull(min(id),(select max(id) from table1 ))

from

( select top (頁大小*(頁數-1)) id from tbl_files order by id desc

) a

) order by id desc

方法三

select top 頁大小 * 

from

(select row_number() over (order by id) as rownumber,* from table1

) awhere rownumber > 頁大小*(頁數-1)

分頁方案二:(利用id大於多少和select top分頁)效率最高,需要拼接sql語句

分頁方案一:(利用not in和select top分頁)   效率次之,需要拼接sql語句

分頁方案三:(利用sql的游標儲存過程分頁)    效率最差,但是最為通用

ps:這三條方案是在網上搜到的,做分頁的時候選用的方案二,發現直接套用語句不能滿足要求,因為根據發帖順序,是需要倒序排列的,就是最新發帖需要排到最前面,於是仿照方案二改成了倒序,希望對大家有所幫助。

SQL三種分頁方案

b 分頁方法 b sql法 儲過程法 游標法。游標耗資 效率低 儲存過程不錯,因為儲存過程是經過預編譯的效率高 靈活。b 這裡介紹sql分頁法 b 方法1 適用於 sql server 2000 2005 select top 頁大小 from table1 where id not in sele...

Django 分頁功能(第三方庫)

1.github搜尋 pure pagination 2.找到jamespacileo django pure pagination 3.pip install django pure pagination 分頁 pure pagination 5.setting.py中配置 分頁 paginati...

AppBoxFuture 整合第三方Sql資料庫

框架設計之初是不準備支援第三方資料庫的,但最近幾個朋友都提到需要將舊的基於傳統sql資料庫的應用遷移到框架內,主要是考慮到一方面目前框架內建的分布式資料庫尚未完善,另一方面是希望能逐步迭代舊應用替換傳統資料庫。因此作者還是決定支援第三方資料庫,下面介紹如何整合第三方資料庫,並將實體模型對映儲存至其中...