---
title: "ORACLE中用rownum分页并排序的SQL语句"
author: "melphi9"
date: "2008-05-16T12:07:13Z"
tags: []
slug: "oracle-rownum-sql"
draft: false
lastmod: "2008-05-16T12:07:13Z"
id: 193590
url: "https://melphi9.iteye.com/blog/193590"
---

# [ORACLE中用rownum分页并排序的SQL语句](http://www.blogjava.net/joessy/articles/1398.html)

以前分页习惯用这样的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上搜到[一篇文章](http://blog.joycode.com/percyboy/archive/2004/09/14/33445.aspx),原来多套一层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

 

写道

SELECT * FROM  
(  
SELECT A.\*, rownum r  
FROM  
(  
SELECT *  
FROM Articles  
ORDER BY PubTime DESC  
) A  
WHERE rownum &lt;= PageUpperBound  
) B  
WHERE r &gt; PageLowerBound;