with temp as ( select goods_id , sum(count) as v from trans group by goods_id having v > 20) select g.id, g.name,g.weight, temp.v from goods as g left join temp on g.id = temp.goods_id where g.weight <50