with a as(select g.id,g.name,sum(t.count) as cc from goods as g join trans as t on g.id=t.goods_id group by g.id ) select goods.id,goods.name,goods.weight,a.cc as total from goods join a on goods.id=a.id where a.cc>20 and goods.weight<50;