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