pls try: select W.id,W.ss,Q.ss from (select a.id,sum(a.money) as ss from a group by a.id having sum(a.money)<500) W , (select b.id,sum(b.money) as ss from b group by b.id) Q where W.id=Q.id
select a.id, sum(a.money),sum(b.money) from a left join b on a.id = b.id group by a.id having sum(a.money) < 500
(select a.id,sum(a.money) as ss from a group by a.id having sum(a.money)<500) W ,
(select b.id,sum(b.money) as ss from b group by b.id) Q
where W.id=Q.id
on a.id = b.id
group by a.id
having sum(a.money) < 500