Yahia Baiba Yahia Baiba - 2 months ago 6
MySQL Question

sql 3 tables union

I have three tables:

Container
with fields are
ContainerNo
,
Date
,

and a table :
Order
with fields:
ConteunrNo
,
Customer


and the third table is
Purchase
with field
ConteunrNo
,
Customer
.

All tables are linked with the Container No.

I want to make a Union query that show
ConainerNo
,
Customer
.

That will include all record from all tables.

And that will work with php later because I heard some complaining about union queries working in phpmyadmin but not in PHP
Thank you

Answer

You can use a union all a an inner join

select ContainerNo , Customer
from Order 
inner join  Container.ContainerNo =   Order.ContainerNo 
union all
select ContainerNo , Customer
from Purchase 
inner join  Container.ContainerNo =   Purchase.ContainerNo