1

这是我的查询:

select * 
  from (SELECT "MYTABLE".* FROM "MYTABLE" ORDER BY "COMPANY" ASC, "SURNAME" ASC) 
 where ROWNUM between 0 and 20

效果如预期,但是当我尝试获取接下来的 20 条记录时:

select * 
  from (SELECT "MYTABLE".* FROM "MYTABLE" ORDER BY "COMPANY" ASC, "SURNAME" ASC) 
 where ROWNUM between 20 and 40

结果集是空的?为什么会这样,我该怎么做才能获得接下来的 20 条记录?

4

1 回答 1

4

您必须嵌套 rownum ,因为 rownum 仅在获取行后才会增加。所以说哪里rownum > 1永远不会返回一行。例如

select *
  from (select a.*, rownum r 
          from (select mytable.*
                  from mytable 
                 order by company asc, surname asc) a
         where rownum <= 40
       )
 where r >= 20
 order by r;

或者你可以使用row_number()分析

select *
  from (select mytable.*, row_number() over (order by company asc, surname asc) rn
          from mytable) 
 where rn between 20 and 40
 order by rn;
于 2013-03-05T12:31:32.400 回答