I have two tables:
default:
+----+--------+
| id | colour |
+----+--------+
| 1 | red |
| 2 | green |
| 3 | yellow |
+----+--------+
custom:
+--------+--------------+---------+
| linkId | customcolour | ownerId |
+--------+--------------+---------+
| 1 | bright red | 1 |
| 2 | garden green | 2 |
+--------+--------------+---------+
I want to return everything from the default table and then get any associated customcolours (via the linkId). The query I am using is:
SELECT a.colour, b.customcolour
FROM default a
LEFT JOIN custom b ON a.id = b.linkId
WHERE (b.ownerId IS NULL OR b.ownerId = 1)
GROUP BY a.id ORDER BY a.colur
However when I join to the custom table it will not select custom linkId 2 because the ownerId is not 1 nor NULL. Is there a way to return the row default.id = 2 and just set customercolour as NULL, without adding it to the table?
You need to move your WHERE criteria into the join