MySQL LEFT JOIN – Values can be NULL

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, MySQL Tutorial  //  No Comments

I’m modifying a MySQL statement but having a few troubles with LEFT JOIN and blank rows.

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:

SELECT cs.case_id, cs.child_firstname, gu.*
   FROM tblcases cs 
        LEFT JOIN tblguardians gu
        ON cs.case_id = gu.case_id 
    AND gu.firstname LIKE '%sally%' /* <- this line was moved up from WHERE */

  WHERE cs.child_firstname = '%craig%' 

Leave a comment

Share This Post

RSS Wordpress News