# 直接表关联+分组,用易于理解的方式
select
cust_id,
sum(total_price) as total_ordered
from
(
select -- 查询范围是关联后的大表,可以选择各个关联表进入大表的列名或进行运算。
cust_id,
(item_price*quantity) as total_price
from
OrderItems a right join Orders b
on a.order_num = b.order_num -- 关联两张表,对于关联字段(列名)下,两表共有的单元,
) as c -- 该单元所在的一整行(含对应列名)都会进入大表。
group by cust_id
order by total_ordered desc
;

京公网安备 11010502036488号