I have a problem figuring out a small problem with this query to mysql db
SELECT s.*
FROM section AS s, sectioncompany AS scom
WHERE ((s.restricted = 0 ) OR (s.restricted = 1 AND s.id = scom.sectionId AND scom.companyId = $companyId))
This works fine and returns results when there are entries in sectioncompany table, but when that table is empty, query won’t result anything. At the moment I did a workaround by simply adding an empty record to sectioncompany table but I want to understand what have I overlooked. As you can see, the condition that involves existing values in that table wouldn’t be met once table is empty, but it should still return something for s.restricted = 0, shouldn’t it? What am I missing?
Thanks
The problem here is that you’re using a INNER JOIN (by using the WHERE clause to match on (s.id = scom.sectionId). What you want is a LEFT JOIN: