數據加載中……
          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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 桓台县| 福泉市| 青田县| 浦城县| 聂荣县| 聂拉木县| 柞水县| 江孜县| 名山县| 蓬莱市| 安新县| 天柱县| 民权县| 屏东县| 富平县| 阿鲁科尔沁旗| 九江市| 黑水县| 建湖县| 汕尾市| 河间市| 嘉善县| 松潘县| 兰州市| 乌拉特中旗| 大理市| 宽甸| 辽宁省| 南昌市| 濮阳县| 古田县| 柘荣县| 峡江县| 内黄县| 内乡县| 长宁区| 南开区| 张家口市| 古浪县| 顺昌县| 图木舒克市|