order by 使用技巧

2021-07-06 10:40:57 字數 2663 閱讀 9176

//有時候,我們會將進過排序(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排序和分組子句 

group by與order by同時使用

orderby子句中的列必須包含在聚合函式或groupby子句中。eg sql view plain copy select col1 col2 max col3 from tb group by col1 col2 order by col1 col2 max col3 select col1 c...

group by和order by配合使用注意

對於sql server來說 group by和order by同時存在的情況是,order by對group by後的結果再進行排序的,所以order by後面的排序字段需要在select裡出現的,即orderby子句中的列必須包含在聚合函式或groupby子句中。如 下面這個就是錯的 sql c...

SQL的ORDER BY 的使用注意

專案中我寫了一條sql語句 select s.student number as studentnumber,s.name,s.class.name as studentclass,m.name as professiona,d.name as department,c.name as colleg...