如何获得每个客户的工作时间总和? 我的数据看起来像这样
Customer | Hours
C1 | 15
C1 | 13
C3 | 23
C4 | 10
C4 | 5
Customer 和 Hours 在单独的表中。 我是这样查询的:
SELECT DISTINCT t2.Customer
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
WHERE t2.Customer is not null
ORDER BY t2.Customer ASC
结果是
Customer
C1
C3
C4
我接下来要做的是对客户的工作时间求和,这样输出将如下所示:
Customer | Hours
C1 | 28
C3 | 23
C4 | 15
最佳答案
你想要一个group by
语句,像这样:
SELECT t2.Customer, sum(t1.hours) as hours
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE t2.Customer is not null
GROUP BY t2.Customer
ORDER BY t2.Customer ASC;
关于mysql - 获取客户行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144829/