题解 | #作为计算字段使用子查询#
从 Products 表中检索所有的产品名称以及对应的销售总数
https://www.nowcoder.com/practice/2b289b78de1546f38fd24e17e56f1bec
select prod_name ,(select sum(quantity) from OrderItems where OrderItems.prod_id = Products.prod_id group by OrderItems.prod_id) as quant_sold from Products;
题目:编写 SQL 语句,从 Products 表中检索所有的产品名称(prod_name),以及名为 quant_sold 的计算列,其中包含所售产品的总数(在 OrderItems 表上使用子查询和 SUM(quantity)检索)。
检索对象: products表的prod_name 加 计算列quan_sold
计算列:要计算sum(quantity),用子查询表示,即:select sum(quantity)from OrderItems where 表一.ID列 = 表二.ID列,group by 订单号。