sql 分页查询

---sql 分页查询
create table Paging(
ID  int null,
Num int null)
declare @i as int
set @i=1000
while @i<10000
begin
insert into Paging
select @i,floor(rand()*20)
set @i=@i+1
end
select * from Paging
--每页10条,取第800页数据
--方法一:Order by
Select Top (40-31+1)  * From Paging Where ID in (Select Top 8010 ID From Paging Order by ID  ) Order by ID Desc
--方法二:not in/top
select top 10 * from Paging where id not in (select top 8000 id from Paging order by id) order by id
--方法三:not exists 
select top 10 * from Paging where not exists 
(select 1 from (select top 8000 id from Paging order by id)a1 where a1.id=Paging.id) order by id
--方法四:max/top
select top 10 * from Paging where id>(select max(id) from (select top 8000 id from Paging order by id)a1) order by id
--方法五:row_number()
select * from (select row_number()over(order by id)rownumber,* from Paging)a1 where rownumber>30 and rownumber<41
--方法六:row_number() 变体,不基于已有字段产生记录序号,先按条件筛选以及排好序,再在结果集上给一常量列用于产生记录序号
select * from (select row_number()over(order by id)rownumber,* from (select top 40 * from Paging where 1=1 order by id)a
)b where rownumber>30


exec pag_1 201
--分页,每页20条数据存储过程
create proc pag_1
@i int
as
begin
select ID,Num from (select ROW_NUMBER()over(order by ID) nume,* from paging)a
where a.nume>(@i-1)*20 and a.nume<(@i*20)+1
end

 

posted @ 2019-05-24 16:24  ZHOUZC  阅读(4076)  评论(0编辑  收藏  举报