ORACLE中用rownum分頁(yè)并排序的SQL語句

          以前分頁(yè)習(xí)慣用這樣的SQL語句:

          select * from

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

          where b.row_num between 1 and 10

          結(jié)果發(fā)現(xiàn)由于該語句會(huì)先生成rownum 后執(zhí)行order by 子句,因而排序結(jié)果根本不對(duì),后來在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 工作日志 閱讀(27926) 評(píng)論(14)  編輯  收藏 所屬分類: oracle相關(guān)
           
          Comments
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          謝謝,受益匪淺
          Posted @ 2006-11-08 08:19  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的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  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          見識(shí)過了
          Posted @ 2007-09-12 11:37  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句[未登錄]
          恩 是正確的 我也這么寫的 不過速度不怎么地 這種寫法50W數(shù)據(jù)如果字段多的話要10多秒 不知哪位高手正點(diǎn)高效的
          Posted @ 2008-12-29 16:18  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的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  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          將select 后面的*再改寫為具體的column name,能好點(diǎn)!
          Posted @ 2009-09-04 17:17  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          @tang6704sadasd撒旦撒大大的撒
          Posted @ 2010-03-29 14:58  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          @wxy


          很對(duì)
          Posted @ 2010-06-27 12:11  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句[未登錄]
          其實(shí)你可以再試試,第一種根本不是按照rownum排序的
          Posted @ 2010-08-04 00:32  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句[未登錄]
          非常感謝。
          Posted @ 2011-07-10 17:12  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          @Sam
          呵呵
          Posted @ 2011-07-10 21:25  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          order字句可以直接用于
          select * from
          (
          select t.*,rownum row_num from mytable t order by t.id desc)

          ) where row_num between 1 and 10
          我怎么可以呢?剛剛測(cè)試了下
          Posted @ 2013-03-27 13:40  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          @榆戀蝶
          你的不可以的。你可以多做些數(shù)據(jù),然后你會(huì)發(fā)現(xiàn)第一頁(yè)和第二的數(shù)據(jù)有什么區(qū)別
          Posted @ 2013-08-15 18:41  回復(fù)  更多評(píng)論    
          # re: ORACLE中用rownum分頁(yè)并排序的SQL語句
          @Ringer'
          這個(gè)其實(shí)是可以的 現(xiàn)在oracle10g已經(jīng)可以了 并且無需在外面套那么多 一層也可以搞定
          Posted @ 2013-09-03 17:08  回復(fù)  更多評(píng)論    
           

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 鹤壁市| 凉城县| 常熟市| 噶尔县| 绵阳市| 慈利县| 奉贤区| 永定县| 和顺县| 横峰县| 扎囊县| 宾阳县| 乐东| 嘉定区| 沭阳县| 武冈市| 大方县| 长宁县| 康马县| 山丹县| 茂名市| 简阳市| 祁东县| 中卫市| 库尔勒市| 同江市| 色达县| 依兰县| 罗定市| 怀仁县| 吉隆县| 邢台县| 乐平市| 稻城县| 台南市| 河津市| 绥中县| 罗源县| 石首市| 会同县| 荆门市|