I would like to create a view from MySQL with 4 tables :
Customer
customer_id | lastname |
---|---|
1 | Doe |
2 | Smith |
Order
order_id | date | customer_id |
---|---|---|
1 | 2021-09-15 | 1 |
2 | 2021-09-12 | 1 |
Product
product_id | ref | price |
---|---|---|
1 | 0001 | 15.00 |
2 | 0002 | 17.00 |
3 | 0003 | 13.50 |
product_to_order
product_id | order_id | quantity |
---|---|---|
1 | 1 | 2 |
2 | 1 | 1 |
1 | 2 | 3 |
As you can see, there's no order for customer #1 and no order for product #3.
I would like to create a full view for the whole datas in the base as :
lastname | order_date | product_ref | price | quantity |
---|---|---|---|---|
Doe | 2021-09-15 | 0001 | 15.00 | 2 |
Doe | 2021-09-15 | 0002 | 17.00 | 1 |
Doe | 2021-09-12 | 0001 | 15.00 | 3 |
NULL | NULL | 0003 | 13.50 | NULL |
Smith | NULL | NULL | NULL | NULL |
So, i try to use some outer joins to get what i want :
SELECT c.lastname lastname, o.date order_date,
p.ref product_ref, p.price price, po.quantity quantity
FROM customer c
LEFT JOIN òrder`o ON c.customer_id = o.customer_id
NATURAL JOIN product_to_order po
RIGHT JOIN product p ON po.product_id = p.product_id;
But got only products that was not in an order and not the customer.
Try to intersect or union but not successfully.
What i'm missing ?
Regards