主要是理解题干:找购买个数超过20,质量小于50的商品,按照商品id升序排序

第一,将两个表进行联结 FROM goods join trans on ...

第二,通过WHERE语句,筛选出“质量小于50的商品”

第三,对goods.id进行GROUP BY 分组,进行聚合求和sum(count),再通过HAVING 筛选出“购买个数超过20”

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