2015-05-19 75 views
3

我有2個查詢:結合2個SQL查詢中1個表

查詢1:

SELECT w2.Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in ,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone 
FROM w2 
INNER JOIN client 
ON w2.Don_Ordre = client.Client 
WHERE client.Zone='CLR' 
SORT BY w2.Avis ; 

查詢2:

SELECT w3.Avis,w3.Cde_Sap,w3.Don_Ordre,w3.PN_in , w3.SN_in,w3.DATE2,w3.Statut_Cde,client.Zone 
FROM w3 
INNER JOIN client 
ON w3.Don_Ordre = client.Client 
WHERE client.Zone='CLR' 
SORT BY w3.Avis; 

我想在1頁顯示這些2次的查詢和按Avis排序。它不工作。

+1

UNION ALL,在派生表。 ORDER BY的結果? – jarlh

+0

@jarlh你可以幫助代碼。我是MySQL新手。 –

回答

1

UNION ALL,在派生表中。 ORDER BY該結果:

select * from 
(
SELECT w2.Avis as Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in ,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone 
FROM w2 
INNER JOIN client 
ON w2.Don_Ordre = client.Client 
WHERE client.Zone='CLR' 

UNION ALL 

SELECT w3.Avis,w3.Cde_Sap,w3.Don_Ordre,w3.PN_in , w3.SN_in,w3.DATE2,w3.Statut_Cde,client.Zone 
FROM w3 
INNER JOIN client 
ON w3.Don_Ordre = client.Client 
WHERE client.Zone='CLR' 
) dt 
order by avis 

或者:

SELECT w2.Avis,w2.Cde_Sap,w2.Don_Ordre,w2.PN_in,w2.SN_in,w2.DATE2,w2.Statut_Cde,client.Zone 
FROM (select * from w2 
     UNION ALL 
     select * from w3) as w2 
INNER JOIN client 
ON w2.Don_Ordre = client.Client 
WHERE client.Zone='CLR' 
SORT BY w2.Avis ; 
+0

至少在ANSI SQL中,只有一個Avis列,從1:st SELECT命名。這不適用於MySQL嗎?無論如何,我會添加一個列別名。 – jarlh