题解 | SQL必知必会 T97
返回顾客名称和相关订单号以及每个订单的总价
https://www.nowcoder.com/practice/4dda66e385c443d8a11570a70807d250
select c.cust_name, o.order_num, sum(oi.item_price * oi.quantity) OrderTotal from Orders o inner join Customers c on c.cust_id = o.cust_id inner join OrderItems oi on o.order_num = oi.order_num group by o.order_num, c.cust_name order by cust_name asc, o.order_num asc
思路:
1.三张表中orders表的变量能够分别在其他两张表中找到对应的,所以将其他两张表加入到orders中来写比较容易理解。
2.加总的时候,要注意按照订单号order_num与人名,不然会出现加总错误。
3.先group后order。先排序的放在前面。