sql 分页

page 当前页,pageSize 每页大小

1、

order by [字段] offset [preIndex] rows fetch next [rowNum] rows only

select * from (
    select row_number() over(order by createTime desc) as id, *
    from test
)  tmp
order by id offset 10 rows fetch next 5 rows only;

总结:这种方式的起始值与结束值计算方式: offset (当前页数-1)*页大小+1   rows fetch next 页数 rows only

order by id offset page-1)*pageSize rows fetch next pageSize  rows only;

2. row_number() over()加序号,通过序号取分页

下面以取第 11 - 15 的数据为例

select *
    from
    (
    select row_number() over(order by createTime  desc) as id, *
    from test
    )tmp 
where id between 11 and 15

总结: 这种方式采用    RowId BETWEEN (当前页数-1)*页大小+1  and  页数*页大小   ,而且包含起始值与结束值

where id between (page-1)* pageSize+page* pageSize 
posted on 2024-07-26 16:56  michellexiaoqi  阅读(28)  评论(0编辑  收藏  举报