- 先对trans表中每个商品的购买数量的求和。
- 将trans表与goods表用商品id进行连接。
- 利用where语句进行筛选,并用id进行排序。
select g.id, g.name, g.weight,a.total
from goods g join (
select t.goods_id, sum(t.count) as total
from trans t
group by t.goods_id
) a
on g.id = a.goods_id
where g.weight < 50
and a.total > 20
order by id