數據加載中……
          oracle中rownum的用法
                 最近在做一個項目,因為考慮的主要是實現查詢,所以沒有用到Hibernate。直接用的jdbc,里面涉及到分頁,所以用到rownum了。
               比如,寫個最簡單的用法:select *from (select *from adjustrequsition a order by a.applydate desc) where rownum<6;這樣才是正確的想法,往往像我這樣的新手,喜歡這樣寫:select *from adjustrequsition a where rownum<6 order by a.applydate desc; 這樣是最容易范的錯誤。。因為rownum是先從數據庫中任意取的數據,然后在按條件排序。。HOHO。。
               下面是我寫的我工作4個月來最長的sql代碼,畢竟我不是DBA哦。。呵呵
              

          select *
           from (select row_number() over(order by t.BEGIN_DATE) ranging,
                         decode(action_seq,
                               
          2,
                                t.person_name,
                               
          3,
                                (select name from account where id = s1.main_account_seq),
                                (select name from account where id = s1.main_account_seq)) as debit_name,
                         decode(action_seq,
                               
          7,
                                t.person_name,
                               
          4,
                                (select name from account where id = s1.main_account_seq),
                                (select name from account where id = s2.main_account_seq)) as credit_name,
                         (SELECT action_name FROM action_type WHERE t.action_seq = ID) AS action_name,
                         decode(action_seq,
                               
          2,
                                decode(bank_seq,
                                       null,
                                      
          '郵局',
                                       (select bank_name
                                          from bank_info
                                         where id = t.bank_seq)),
                                (select bank_name from bank_info where id = t.bank_seq)) bankname,
                         decode(action_seq,
                               
          2,
                                t.card_no,
                                (select decode(a.email, null, a.mobile, a.email)
                                   from account a, sub_account s
                                  where a.id = s.main_account_seq
                                    and s.id = t.debit_seq)) as debit_no,
                         decode(action_seq,
                               
          4,
                                t.card_no,
                               
          7,
                                t.card_no,
                                (select decode(a.email, null, a.mobile, a.email)
                                   from account a, sub_account s
                                  where a.id = s.main_account_seq
                                    and s.id = t.credit_seq)) as credit_no,
                         t.amount,
                         to_char(t.BEGIN_DATE,
          'YYYY-MM-DD hh24:mi') as begin_date,
                         t.remark,
                         t.id,
                         t.voucher_code,
                         t.DEBIT_FEE,
                         t.CREDIT_FEE
                    from transaction t, sub_account s1, sub_account s2
                   where t.voucher_code is not null
                  
                     and exists
                   (select s.id
                            from account a, sub_account s
                           where s.main_account_seq = a.id
                             and a.account_type =
          'B'
                             and (t.credit_seq = s.id or t.debit_seq = s.id))
                     and t.DEBIT_SEQ = s1.ID
                     and t.CREDIT_SEQ = s2.ID
                     and t.action_seq =
          3)
           where ranging between
          1 and 100

          posted on 2007-08-17 16:08 flyleer 閱讀(357) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 禹州市| 开江县| 新龙县| 陆丰市| 旅游| 大港区| 鄱阳县| 梓潼县| 宝兴县| 昌都县| 涿鹿县| 图木舒克市| 广汉市| 安远县| 虞城县| 新巴尔虎左旗| 靖安县| 桐乡市| 交城县| 库尔勒市| 浦县| 鸡东县| 马关县| 南城县| 黄平县| 南开区| 万载县| 成都市| 比如县| 安新县| 海伦市| 商水县| 来宾市| 遵义市| 隆子县| 宜州市| 山东省| 安远县| 天镇县| 陈巴尔虎旗| 沈阳市|