在1个表中组合2个SQL查询

时间:2015-05-19 09:27:22

标签: mysql sql isqlquery

我有2个查询:

查询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页和SORT BY Avis中显示这2个查询。它不起作用。

1 个答案:

答案 0 :(得分: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 ;