I have the following structure
COFFEE ORDER_ITEMS ORDERS USERS
------ ----------- ------ -----
id order_id id id
name coffee_id user_id email
There is a many to many relation between orders and coffees. And a one to many relation between users and orders. I would like to know what coffees a user has ordered.
I have a query to retrieve the orders per user:
SELECT users.id as USERID, orders.id as ORDERID
FROM users
INNER JOIN orders on users.id = orders.user_id
As an example this gives me:
userid orderid
1 1
1 2
2 3
4 4
I have a query to retrieve the coffees that belong to an order:
SELECT orders.id, coffees.name
FROM orders, coffees, order_coffees
WHERE coffees.id = order_coffees.coffee_id
AND orders.id = order_coffees.order_id
As an example, this gives me:
orderid coffeename
1 Coffee A
1 Coffee B
2 Coffee A
2 Coffee C
I would like to get the following:
userid orderid coffeename
1 1 Coffee A
1 1 Coffee B
1 2 Coffee A
2 3 Coffee C
4 4 NULL