题解 | #返回每个顾客不同订单的总金额#

返回每个顾客不同订单的总金额

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;

关联子查询就需要理解他的运算逻辑了,认知难度更高一点,主要是实际中用的上,很难记得牢固

全部评论
社长,第二个使用where子句进行表连接?它与表连接(inner、left)等有什么不一样么
点赞 回复 分享
发布于 2022-09-07 17:43 广东

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务