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 工作日志 阅读(16305) 评论(11)  编辑  收藏 所属分类: 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  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
将select 后面的*再改写为具体的column name,能好点!
Posted @ 2009-09-04 17:17  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
@tang6704sadasd撒旦撒大大的撒
Posted @ 2010-03-29 14:58  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
@wxy


很对
Posted @ 2010-06-27 12:11  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句[未登录]
其实你可以再试试,第一种根本不是按照rownum排序的
Posted @ 2010-08-04 00:32  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句[未登录]
非常感谢。
Posted @ 2011-07-10 17:12  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
@Sam
呵呵
Posted @ 2011-07-10 21:25  回复  更多评论    
 

博问 - 解决您的IT难题
博客园  博问  IT新闻  Java程序员招聘
标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录      


网站导航: