我已经编写了这个代码,它显示了一个表以及客户花了多少钱。这些数据来自多个表格,并按原样加入。
SELECT c.lastname, c.firstname,
CONCAT(oi.quantity*oi.paideach) as 'Total Spent'
FROM customers c INNER JOIN orders o USING(customer_num)
INNER JOIN orderitems oi USING(order_num)
ORDER BY c.lastname, c.firstname ;
我的问题是,如何将订单汇总在一起以获得订单总额,截至目前我只是得到
_______________________________________________________
| Lastname Firstname Ordertotal |
| Jim Bob 170.93 |
| Jim Bob 15.02 |
| Rob Milford 11.30 |
| Rob Milford 20.59 |
| Rob Milford 59.49 |
|_____________________________________________________|
我真的很沮丧,只需要某种解决方案。我已经尝试在我的concat语句之前添加SUM(),但它让我总结了所有的值,而不是总结每个人的总数。
答案 0 :(得分:1)
您需要使用SUM:
SELECT c.lastname, c.firstname,
SUM(oi.quantity*oi.paideach) as 'Total Spent'
FROM customers c INNER JOIN orders o USING(customer_num)
INNER JOIN orderitems oi USING(order_num)
GROUP BY c.lastname, c.firstname
ORDER BY c.lastname, c.firstname ;