日期:2014-05-17  浏览次数:20891 次

oracle分页问题~~急~~~~~~~
刚学oracle,自己写了个oracle的分页查询
但是效果很不理想,出现了一些错误
请大家帮我看看怎么修改语句才对

------现在表中一共10列数据,分页查询,每页显示5条数据,语句如下---------
--这个是查询第1页数据的语句
select   *   from   (
              select     *   from   (
                              select   *   from   apw_ad   where     active= '1 '     and   rownum <11     order   by   createtime   desc
                )   where   rownum <=   5   order   by   createtime
  )   derivedtbl   order   by   createtime   desc
  ;
--------------
--这个是查询第2页数据的语句
select   *   from   (
              select     *   from   (
                              select   *   from   apw_ad   where     active= '1 '     and   rownum <6     order   by   createtime   desc
              )   where   rownum <=   5   order   by   createtime
  )   derivedtbl   order   by   createtime   desc

-------------------------------------
查询的结果出现了错误,第1页结果是正常的,第2页就不对了
第2页应该显示最后5条数据,但是他显示的却是   第1条数据和最后4条数据
怎么修改这查询语句才能正确?
急~大家帮我看下,谢谢

------解决方案--------------------
select *
from (select *
from (select ap.*, rownum rn
from apw_ad ap
where ap.active = '1 '
order by ap.createtime desc)
where rn <= 10)
where rn > 5;

------解决方案--------------------
select * from (
select * from (
select rownum rn,apw_ad.* from apw_ad where active= '1 ' and rownum <=page*5 order by createtime desc
) where rn > (page-1)*5 and rn <=page*5 order by createtime
) derivedtbl order by createtime desc

page是你当前页码.