# 法1 直接使用子查询
select prod_name,
(select sum(quantity)
# from OrderItems ,Products
from OrderItems
# 不要忘记这样一个 where 等式连接
where OrderItems.prod_id =Products.prod_id
group by prod_id
#取出来OrderItems表中按prod_id分组后的 每一件商品id所对应的quantity和
) AS quant_sold
# 因为外层是from的products,是先执行from 再where,再select
# 你纠结的内层为什么不这样写 是有原因的 已经在外层from了
from Products;
# 思路:作为计算字段使用子查询
# 分组计算销售总数。select sum(quantity) from OrderItems group by prod_id
# 在Products表中搜索。where OrderItems.prod_id=Products.prod_id
# 法2 将2个表连接 将两表进行关联,从关联表中进行查询,最后再分组,依据产品名称进行分组
select prod_name,
sum(quantity)
from Products a, OrderItems b
where a.prod_id=b.prod_id
group by prod_name ;