美文网首页
oracle,mysql,sqlserver分页查询实现

oracle,mysql,sqlserver分页查询实现

作者: 手并夕夕夕夕 | 来源:发表于2018-08-29 17:12 被阅读0次

1.oracle

使用关键字rownum嵌套查询实现:

select * from (

    select a.*, rownum rn from(

        select * from 表名 order by 排序字段) a

    where rownum <= 30)

where rn >= 21;

2.mysql

使用limit函数:

1)取前5条数据

select * from 表名 limit 0,5;

或者

select * from 表名 limit 5;

2)查询第11到第15条数据

select * from 表名 limit 10,5;

3)limit关键字的用法:

LIMIT[offset,]rows

offset指定要返回的第一行的偏移量,rows第二个指定返回行的最大数目。初始行的偏移量是0(不是1)。

3.sqlserver

使用top关键字:

--第一种

select top 10 ID from T where ID not in(select top 30 ID from T ORDER BY ID ASC)ORDER BY ID

--第二种

SELECT * FROM (select top 10 * FROM( select top 40 * from T order by ID asc)TT order by TT.ID DESC)TTT order by TTT.ID asc

--第三种

select * from T where T.ID in( select top 10 ID FROM(select top 40 ID from T order by T.ID asc)TT order by TT.ID desc) order by ID

--第四种

select * from( select ROW_NUMBER() over(order by ID)TT FROM T)TTT WHERE TTT.TT between 30 and 40

相关文章

网友评论

      本文标题:oracle,mysql,sqlserver分页查询实现

      本文链接:https://www.haomeiwen.com/subject/heykwftx.html