It is a common trap: as soon as you add a WHERE
- about a LEFT JOIN
ed table, you logically and implicitely make it an inner join as long as you do not add a hint about NULL.
SELECT * FROM tcredit
LEFT JOIN tcredit_order ON tcredit.id_credit=tcredit_order.id_credit
GROUP BY tcredit.id_credit
HAVING (tcredit.credit > SUM(tcredit_order.credit) or (SUM(tcredit_order) is null))
ORDER BY tcredit.date_limit ASC
That should fix the problem. Remember: 1 > 0
is true, but 1 > NULL
is NULL and not true.
Following the Comment, coalesce
is a bit cooler (I usually use it for longer chains only).
HAVING (tcredit.credit > COALESCE(SUM(tcredit_order.credit),0))
Or if you're afraid your coworkers might not know it simply IFNULL
HAVING (tcredit.credit > IFNULL(SUM(tcredit_order.credit),0))