Welcome 微信登录

首页 / 数据库 / MySQL / 关于Oracle的rownum

1.给12个id按升序排序,取出排序后的前5个id。select * from (查询语句) where rownum <=5Oracle中的rownum的是在取数据的时候产生的序号
  1. select rownum,id from t_busi_presend_mx order by id   
(注:下面结果中,id是已经按照升序排序的)
    ROWNUM    ID
---------- --------------------
         9        20111028094601201693
         8        20111028094601888130
        10       20111028094605946169
         4        20111028094605984087
         7        20111028110758617803
        11       20111028110758930318
         1        20111031064620139463
         2        20111031070024670965
         3        20111031072450370812
         5        20111101112559541615
        12       20111101112605330425
         6        20111101112605775938可以看出,rownum并不是按照id列来生成的序号。系统是按照记录插入时的顺序给记录排的号,rowid也是顺序分配的。为了解决这个问题,必须使用子查询:
  1. select * from (select rownum,id from t_busi_presend_mx order by id ) where rownum<=5  
    ROWNUM    ID
---------- --------------------
         9      20111028094601201693
         8      20111028094601888130
        10     20111028094605946169
         4      20111028094605984087
         7      20111028110758617803可以看出,忽略前面的rownum,后面取出的id,确实是我需要的,排序后的前5个id。
2.更新中间100行数据:
  1. update t_busi_presend_mx set codetype="1" where rowid in (  
  2. select t.row_id from(  
  3. select rownum rn,rowid row_id from t_busi_presend_mx   
  4. ) t where t.rn>=101 and t.rn<=199  
  5. )  
Oracle索引 主键影响查询速度Oracle将字符串转换为数字相关资讯      Oracle数据库  Oracle入门教程  oracle数据库教程 
  • Oracle数据库全球化  (03月01日)
  • Oracle数据库日期过滤方法性能比较  (02/02/2015 13:20:26)
  • Oracle数据库安装中端口被占用问题  (10/29/2014 07:42:24)
  • 在CentOS 6.6上搭建C++运行环境并  (10/10/2015 19:44:40)
  • Oracle数据库无法使用localhost和  (11/14/2014 16:39:10)
  • 使用SQLT来构建Oracle测试用例  (08/28/2014 06:17:41)
本文评论 查看全部评论 (0)
表情: 姓名: 字数