<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    lqxue

    常用鏈接

    統計

    book

    tools

    最新評論

    [精華] [轉貼]如何正確利用Rownum來限制查詢所返回的行數?

    如何正確利用Rownum來限制查詢所返回的行數? 
    軟件環境: 
    1、Windows NT4.0+ORACLE 8.0.4
    2、ORACLE安裝路徑為:C:\ORANT

    含義解釋: 
    1、rownum是oracle系統順序分配為從查詢返回的行的編號,返回的第一行分配的是1,第二行是2,
      依此類推,這個偽字段可以用于限制查詢返回的總行數。
    2、rownum不能以任何基表的名稱作為前綴。 
    使用方法: 
    現有一個商品銷售表sale,表結構為:
    month    char(6)      --月份
    sell    number(10,2)   --月銷售金額

    create table sale (month char(6),sell number);
    insert into sale values('200001',1000);
    insert into sale values('200002',1100);
    insert into sale values('200003',1200);
    insert into sale values('200004',1300);
    insert into sale values('200005',1400);
    insert into sale values('200006',1500);
    insert into sale values('200007',1600);
    insert into sale values('200101',1100);
    insert into sale values('200202',1200);
    insert into sale values('200301',1300);
    insert into sale values('200008',1000);
    commit;

    SQL>; select rownum,month,sell from sale where rownum=1;(可以用在限制返回記錄條數的地方,保證不出錯,如:隱式游標)

       ROWNUM MONTH       SELL
    --------- ------ ---------
            1 200001      1000

    SQL>; select rownum,month,sell from sale where rownum=2;(1以上都查不到記錄)

    沒有查到記錄

    SQL>; select rownum,month,sell from sale where rownum>;5;
    (由于rownum是一個總是從1開始的偽列,Oracle 認為這種條件不成立,查不到記錄)


    沒有查到記錄

    只返回前3條紀錄
    SQL>; select rownum,month,sell from sale where rownum<4;

       ROWNUM MONTH       SELL
    --------- ------ ---------
            1 200001      1000
            2 200002      1100
            3 200003      1200


    如何用rownum實現大于、小于邏輯?(返回rownum在4—10之間的數據)(minus操作,速度會受影響)
    SQL>; select rownum,month,sell from sale where rownum<10
      2  minus
      3  select rownum,month,sell from sale where rownum<5;

       ROWNUM MONTH       SELL
    --------- ------ ---------
            5 200005      1400
            6 200006      1500
            7 200007      1600
            8 200101      1100
            9 200202      1200

    想按日期排序,并且用rownum標出正確序號(有小到大)
    SQL>; select rownum,month,sell from sale order by month;

       ROWNUM MONTH       SELL
    --------- ------ ---------
            1 200001      1000
            2 200002      1100
            3 200003      1200
            4 200004      1300
            5 200005      1400
            6 200006      1500
            7 200007      1600
           11 200008      1000
            8 200101      1100
            9 200202      1200
           10 200301      1300

    查詢到11記錄.

    可以發現,rownum并沒有實現我們的意圖,系統是按照記錄入庫時的順序給記錄排的號,rowid也是順序分配的

    SQL>; select rowid,rownum,month,sell from sale order by rowid;

    ROWID                 ROWNUM MONTH       SELL
    ------------------ --------- ------ ---------
    000000E4.0000.0002         1 200001      1000
    000000E4.0001.0002         2 200002      1100
    000000E4.0002.0002         3 200003      1200
    000000E4.0003.0002         4 200004      1300
    000000E4.0004.0002         5 200005      1400
    000000E4.0005.0002         6 200006      1500
    000000E4.0006.0002         7 200007      1600
    000000E4.0007.0002         8 200101      1100
    000000E4.0008.0002         9 200202      1200
    000000E4.0009.0002        10 200301      1300
    000000E4.000A.0002        11 200008      1000

    查詢到11記錄.

    正確用法,使用子查詢
    SQL>; select rownum,month,sell from (select month,sell from sale group by month,sell) where rownum<13;

       ROWNUM MONTH       SELL
    --------- ------ ---------
            1 200001      1000
            2 200002      1100
            3 200003      1200
            4 200004      1300
            5 200005      1400
            6 200006      1500
            7 200007      1600
            8 200008      1000
            9 200101      1100
           10 200202      1200
           11 200301      1300

    按銷售金額排序,并且用rownum標出正確序號(有小到大)
    SQL>; select rownum,month,sell from (select sell,month from sale group by sell,month) where rownum<13;

       ROWNUM MONTH       SELL
    --------- ------ ---------
            1 200001      1000
            2 200008      1000
            3 200002      1100
            4 200101      1100
            5 200003      1200
            6 200202      1200
            7 200004      1300
            8 200301      1300
            9 200005      1400
           10 200006      1500
           11 200007      1600

    查詢到11記錄.

    利用以上方法,如在打印報表時,想在查出的數據中自動加上行號,就可以利用rownum。

    返回第5—9條紀錄,按月份排序
    SQL>; select * from (select rownum row_id ,month,sell 
      2  from (select month,sell from sale group by month,sell)) 
      3  where row_id between 5 and 9;

        ROW_ID MONTH        SELL
    ---------- ------ ----------
             5 200005       1400
             6 200006       1500
             7 200007       1600
             8 200008       1000
             9 200101       1100



    原文鏈接:http://bbs.chinaunix.net/viewthread.php?tid=261521
    轉載請注明作者名及原文出處

    posted on 2007-08-27 10:28 lqx 閱讀(244) 評論(0)  編輯  收藏 所屬分類: database

    主站蜘蛛池模板: 中文字幕乱码亚洲无线三区| 久久久久亚洲av无码专区导航 | 最新国产成人亚洲精品影院| 最近中文字幕免费2019| 亚洲欧美日韩中文二区| 欧美a级在线现免费观看| 亚洲一级高清在线中文字幕| 成人无码区免费A片视频WWW| 亚洲AV一二三区成人影片| 成人免费男女视频网站慢动作| 456亚洲人成在线播放网站| 日韩免费视频播播| 亚洲成AV人片一区二区| 日韩视频免费在线观看| 亚洲丝袜美腿视频| 美女网站免费福利视频| 色婷婷六月亚洲综合香蕉| 国产中文字幕免费| eeuss免费影院| 亚洲天天在线日亚洲洲精| 麻豆视频免费观看| 亚洲av成本人无码网站| 久久亚洲AV无码西西人体| 久久青草免费91线频观看不卡 | 亚洲一区中文字幕| 日本一道高清不卡免费| 一区二区三区在线免费| 亚洲国产成人久久精品影视 | 国拍在线精品视频免费观看| 亚洲欧美在线x视频| 久久亚洲国产精品五月天婷| 永久在线观看www免费视频| 在线a亚洲老鸭窝天堂av高清| 免费在线观看黄网站| 久久国产乱子伦免费精品| 亚洲爆乳无码专区www| 亚洲精品制服丝袜四区| 99re热免费精品视频观看| 亚洲第一视频在线观看免费 | 久久水蜜桃亚洲AV无码精品| 亚洲成AV人片在|