MySQL - 一组时差的SUM

时间:2013-02-09 16:22:39

标签: mysql sql aggregate-functions

我想总结所有时间差异,以显示志愿者的总工作时数。获得时间差的结果集非常简单:

Select timediff(timeOut, timeIn) 
FROM volHours 
WHERE username = 'skolcz'

给出了按小时计算的时间列表,但是我希望将其总计为总计。

所以如果结果集是:

12:00:00
10:00:00
10:00:00
08:00:00

这将只有40个小时。

这可以做一些事情:

SELECT SUM(Select timediff(timeOut,timeIn) 
FROM volHours 
WHERE username = 'skolcz') as totalHours

3 个答案:

答案 0 :(得分:12)

Select  SEC_TO_TIME(SUM(TIME_TO_SEC(timediff(timeOut, timeIn)))) AS totalhours
FROM volHours 
WHERE username = 'skolcz'

如果不是那么可能:

Select  SEC_TO_TIME(SELECT SUM(TIME_TO_SEC(timediff(timeOut, timeIn))) 
FROM volHours 
WHERE username = 'skolcz') as totalhours

答案 1 :(得分:3)

你几乎可以从 Matthew 得到答案,你需要做的就是添加演员:

Select CAST(SUM(timediff(timeOut, timeIn)) as time) as totalhours
FROM volHours 
WHERE username = 'skolcz'    

答案 2 :(得分:1)

尝试类似的东西

 Select SUM(timediff(timeOut, timeIn)) as total
 FROM volHours 
 WHERE username = 'skolcz'