题解 | #返回每个顾客不同订单的总金额#
返回每个顾客不同订单的总金额
https://www.nowcoder.com/practice/ce313253a81c4947b20e801cd4da7894
select t.cust_id,SUM(quantity*item_price) as total_ordered from Orders t inner join OrderItems t1 on t.order_num=t1.order_num group by cust_id ORDER BY total_ordered DESC
仍然是表链接更简单易理解。
SELECT cust_id, (SELECT SUM(quantity * item_price) FROM OrderItems WHERE OrderItems.order_num = Orders.order_num) AS total_ordered FROM Orders ORDER BY total_ordered DESC;
关联子查询就需要理解他的运算逻辑了,认知难度更高一点,主要是实际中用的上,很难记得牢固