为什么oracle需要三层嵌套来实现分页
看下面这个sql语句
select t2.* from (
select t.*, rownum as row_num from t where rownum<=20 order by ID asc
select t.*, rownum as row_num from t where rownum<=20 order by ID asc
) t2 where t2.row_num>10
order by ID asc
因为在查询的时候,order by 的执行是在 select 之后的,所以在第一层查询中,得到的结果可能是如下
这样的子结果集,在经过第二层过滤的时候,是得不到我们想要的结果的
所以需要用如下的sql语句实现分页排序
select t3.* from (
select t2.*, rownum as row_num from (
select * from t order by t.id asc
) t2 where rownum<=20
) t3
where t2.row_num>11
select t2.*, rownum as row_num from (
select * from t order by t.id asc
) t2 where rownum<=20
) t3
where t2.row_num>11
order by t3.id asc