Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine 2 SQL queries in 1 table

Tags:

sql

mysql

I have 2 Queries :

Query 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 ;

Query 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;

I want to display these 2 queries in 1 Page and SORT BY Avis . Its not working .

like image 764
Anuraag Kapoor Avatar asked Aug 31 '26 13:08

Anuraag Kapoor


1 Answers

UNION ALL, in a derived table. ORDER BY on the result:

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

Alternatively:

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 ;
like image 189
jarlh Avatar answered Sep 03 '26 02:09

jarlh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!