SELECT g.id, g.name, g.weight, sum(t.count) as total
FROM goods g
LEFT JOIN trans t on g.id = t.goods_id
WHERE g.weight<50
GROUP BY g.id
HAVING total>20
ORDER BY g.id