2016-12-07 76 views
0

我有一個問題,如何在DB2中進行排序任何人都有什麼想法?如何通過DB 2中的聯合查詢進行排序

選擇COL1,從 工會選擇COL1 COL2,COL2從B

BEFORE 
----Result---- 
col1 col2 
a  abu 
b  bala 
d  daus 
f  faiz 
h  haikal 
c  canon 
e  elly 
g  gous 

AFTER 
----Result---- 
col1 col2 
a  abu 
b  bala 
c  canon 
d  daus 
e  elly 
f  faiz 
g  gous 
h  haikal 
+0

在末尾添加'ORDER BY col1'。 – jarlh

回答

-1

使用子查詢,你可以這樣做:

SELECT * FROM( 選擇COL1,COL2從工會選擇col1,col2 from b )table1 order by table1.col1

0

Jarlh的評論是正確的...

select col1,col2 
from a 
union 
select col1,col2 from b 
ORDER BY col1,col2 

只允許使用單個ORDER BY,因爲它適用於整個結果集。

0

指定列名訂購只需

select col1,col2 
from a 
union 
select col1,col2 from b 
ORDER BY col1,col2 

或指定排名列訂購

select col1,col2 
from a 
union 
select col1,col2 from b 
ORDER BY 1, 2 

注:如果col1中具有獨特的價值(由爲例,如果COL1是一個關鍵列),您應使用union all而不是union。 「聯盟」刪除雙倍,然後「聯合所有」更好的性能

select col1,col2 
from a 
union all 
select col1,col2 from b 
ORDER BY col1,col2 
相關問題