I’m modifying a MySQL statement but having a few troubles with LEFT JOIN and blank rows. At the moment I have:
SELECT cs.case_id, cs.child_firstname, gu.*
FROM tblcases AS cs
LEFT JOIN tblguardians AS gu
ON cs.case_id = gu.case_id
WHERE cs.child_firstname = '%craig%' AND gu.firstname LIKE '%sally%'
This query works fine as long as there is an entry in the “tblguardians” table, but the LEFT JOIN returns NULL if there is no record – therefore the entry isn’t returned when I iterate over the results in PHP.
Is there a way to return the fields from “tblcases” whether or not “tblguardians” has an associated result? I think I’ve read too many pages of Google for much more information to sink in!
Putting a condition on a joined table in your WHERE clause effectively makes that join an INNER JOIN.
To avoid doing that you have to add the second condition to your LEFT JOIN: