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 工作日志 阅读(27899) 评论(14)  编辑  收藏 所属分类: 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  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
order字句可以直接用于
select * from
(
select t.*,rownum row_num from mytable t order by t.id desc)

) where row_num between 1 and 10
我怎么可以呢?刚刚测试了下
Posted @ 2013-03-27 13:40  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
@榆恋蝶
你的不可以的。你可以多做些数据,然后你会发现第一页和第二的数据有什么区别
Posted @ 2013-08-15 18:41  回复  更多评论    
# re: ORACLE中用rownum分页并排序的SQL语句
@Ringer'
这个其实是可以的 现在oracle10g已经可以了 并且无需在外面套那么多 一层也可以搞定
Posted @ 2013-09-03 17:08  回复  更多评论    
 

只有注册用户登录后才能发表评论。


网站导航: