题解 | #返回顾客名称和相关订单号以及每个订单的总价#
返回顾客名称和相关订单号以及每个订单的总价
http://www.nowcoder.com/practice/4dda66e385c443d8a11570a70807d250
- 先两个表用cust_id连接一下,再与第三个表用order_num连接,然后按条件查询。
- 由于计算的是每一个订单的总金额,所以不需要分组。
SELECT temp.cust_name, temp.order_num, (quantity* item_price) AS OrderTotal
FROM
(SELECT cust_name, order_num
FROM Customers c
JOIN Orders o ON c.cust_id = o.cust_id) AS temp
JOIN OrderItems ot ON temp.order_num = ot.order_num
ORDER BY cust_name, order_num ASC;