ORACLE中用rownum分页并排序的SQL语句

以前分页习惯用这样的SQL语句:

select * from

(select t.*,rownum row_num from mytable t order by t.id) b

where b.row_num between 1 and 10

结果发现由于该语句会先生成rownum 后执行order by 子句,因而排序结果根本不对,后来在GOOGLE上搜到一篇文章,原来多套一层select 就能很好的解决该问题,特此记录,语句如下:

select * from

(select a.*,rownum row_num from

(select * from mytable t order by t.id desc) a

) b where b.row_num between 1 and 10

posted on 2005-02-21 17:15 工作日志 阅读(4087) 评论(5)  编辑  收藏 所属分类: oracle相关
 
Comments
# re: ORACLE中用rownum分页并排序的SQL语句
谢谢,受益匪浅
Posted @ 2006-11-08 08:19  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
it's great! your solution is the best one i have seen until now.thank u very much and reaaly appreciate you generation!
Posted @ 2006-11-23 13:24  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
见识过了
Posted @ 2007-09-12 11:37  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句[未登录]
恩 是正确的 我也这么写的 不过速度不怎么地 这种写法50W数据如果字段多的话要10多秒 不知哪位高手正点高效的
Posted @ 2008-12-29 16:18  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
select * from

(select a.*,rownum row_num from

(select * from mytable t order by t.id desc) a
where rownum<=10

) b where b.row_num >= 1
Posted @ 2009-03-04 11:22  回复  更多评论    
 


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交