I have the following SQL Query wich selects a result from 3 tables ‘work’, ‘media’ and ‘work_media’
I want all the work with 1 of its associated media, the following SQL query does that.
But it only return the work that actually has media linked with it and thats’s not what I want, I also want the work returned that has no media attached to it.
How can I accomplisch that?
SELECT work.id, work.title, work.file_name_thumb
FROM work, media, media_work
WHERE media_work.work_id = work.id
AND media_work.media_id = media.id
GROUP BY work.id ORDER BY work.id DESC
You should use OUTER JOIN:
LEFT OUTER JOIN will include all rows from the table on its left even if there is no corresponding row in the right table. Be careful though, that the missing row will be filled with NULLs.