SELECT *
FROM `user`
LEFT JOIN `user_group`
ON `user_group`.`userid` = `user`.`userid`
LEFT JOIN `email_template`
ON `email_template`.`user_id`=`user`.`userid`
WHERE `user`.`agent_id`='123'
AND `email_template`.`type`='advertise'
That is my sql statement. I am joining tables user, user group and email_template. The problem is for the user I am querying there is no email_template of type advertise in the table so the entire query returns no rows. Instead I want 1 row, with the email_template type column null.
Did you try moving the
email_templatefilter to the join condition?Doing this applies the filter on the join instead of the
WHEREclause. If you apply the filter on theWHEREthen you are basically performing anINNER JOINwhich will cause no rows to be returned if nothing meets the criteria: