The title may sound a bit confusing, but let me explain:
I have three tables:
store
storeID | name
1 | store1
2 | store2
3 | store3
product
productID | name
1 | ball
2 | cup
store_product
storeID_fk | productID_fk
2 | 1
1 | 2
3 | 2
What I want to archive is a result like the following:
storeID | name | storeID_fk | productID_fk
1 | store1| NULL | NULL
2 | store2| 2 | 1
3 | store3| NULL | NULL
What I have tried so far:
SELECT * FROM `store`
LEFT JOIN `store_product` on storeID = storeID_fk
WHERE productID_fk = 1;
But this only returns:
storeID | name | storeID_fk | productID_fk
2 | store2| 2 | 1
How can I also display the empty/not existing rows?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
SELECT
*
FROM
`store`
LEFT JOIN
`store_product`
ON storeID = storeID_fk
WHERE
productID_fk = 1