1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        Oracle union all和order by一起使用

        發布時間:  2012/8/21 17:08:45

        //有時候,我們會將進過排序(order by)后的結果集與    
        //其他經過排序的結果集進行合并(union or union all)    
        //比如:    
        select * from tb where length(id)=5 order by id desc    
        -
         

        union all    
        select * from tb where length(id)=10 order by id asc    
        //通常情況下,上面的查詢將會得到下面的錯誤提示:    
        //ORA-00933: SQL command not properly ended    
        //錯誤指向union關鍵字這里    
        //下面我們來看一個具體的實例:    
        //    
        create table t as    
        select 'china' col_1,'america' col_2,'canada' col_3,-1 status from dual union all    
        select '花生','瓜子','綠豆',0 from dual union all    
        select '牙膏','牙刷','杯子',3 from dual union all    
        select '芍藥','牡丹','月季',1 from dual union all    
        select '優樂美','香飄飄','炸雞',2 from dual    
        /    
        //需求:    
        //有如上表t,status字段的取值范圍:[-1,3]    
        //我們想要做的是,按照這樣的方式排序0,1,2,3,-1    
        //    
        //解法:    
        //更具題義,我們需要將status分為兩個區域(>0 和<0)    
        //然后分別對每一個區域內的數據進行order by排序    
        //于是有下面的查詢    
        select col_1,col_2,col_3,status    
        from t    
        where status >= 0     
        order by status  --1    
        union    
        select col_1,col_2,col_3,status    
        from t    
        where status < 0    
        order by status  --2    
        /    
        //不幸的是,正如剛剛開始時我提示的一樣,我們得到了下面的錯誤提示:    
        //ORA-00933: SQL command not properly ended    
        //如果將第一個select語句的order by子句去掉,得到的又不是我們想要的結果    
        //如果將兩個排序子句都去掉的話,雖然按照status為正負數分開了,但是沒有排序    
        //下面我們來看看正確的答案吧!  
        //解法一:  
        select * from (    
               select col_1,col_2,col_3,status    
               from t    
               where status >= 0    
               order by status)    
        union all    
        select * from (    
               select col_1,col_2,col_3,status    
               from t    
               where status < 0    
               order by status)    
        /    
        COL_1  COL_2   COL_3      STATUS    
        ------ ------- ------ ----------    
        花生   瓜子    綠豆            0    
        芍藥   牡丹    月季            1    
        優樂美 香飄飄  炸雞            2    
        牙膏   牙刷    杯子            3    
        china  america canada         -1   
        //解法二:  
        select * from t   
        order by   
              decode(status,  
                     -1,1,  
                     3,2,  
                     2,3,  
                     1,4,  
                     0,5) desc  
        /  
        //這可是一個很妙的排序,本人首次看到在order by語句中可以使用decode()函數來排序  
        //同理,我們也可以使用case語句來排序:  
        //解法三:  
        select * from t   
        order by   
              case status  
              when -1 then 5  
              when 3 then 4   
              when 2 then 3   
              when 1 then 2   
              else 1  
              end   
        /  
        //union 和union all中都支持order by和group by排序和分組子句 
         


        本文出自:億恩科技【www.endtimedelusion.com】

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          0
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线