SELECT tb1.id, tb1.count, SUM(tb2.count) as used FROM tb1 LEFT JOIN tb2 ON tb2.tb1_id = tb1.id GROUP BY id, count HAVING count > used ;