oracle使用rownum进行分页查询

在oracle中没有top函数,不能像MySQL或者是SQLserver那样进行分页查询,但是也提供了其它的方式,比如说rownum,rownum的作用和top类似,是行标记数。

创建一张表:A

CREATE TABLE A(
    ID INT,
    NAME VARCHAR2(20)
);

并且插入一些数据

分页查询也有几种方式:

(1)不排序分页查询,效率高。

例如:

  查询表A中的数据,且规定每一页5条数据,那么查询第二页的分页查询SQL如下:

select * from (select rownum as rowno,tmp.* from A tmp where rownum<=2*5) B where b.rowno>5*(2-1);

(2)带有排序的分页查询,效率较不排序要低

  在和1同意的条件下,但是查询的数据按照ID进行升序排序。

      思路分析:

    1、先排序

select * from a order by id asc;

    2、再进行分页查询

select * from (select rownum as rowno,t.* from  (select * from a order by id asc) t where ROWNUM<=5*2)S where s.rowno>5*(2-1);
原文地址:https://www.cnblogs.com/yuanshuang-club/p/13792115.html