8

我想总结所有时间差异以显示志愿者工作的总小时数。获得时间差异的结果集很容易:

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

?

4

3 回答 3

17
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
于 2013-02-09T16:28:06.143 回答
3

您几乎可以从Matthew那里得到答案,您需要做的就是添加 cast :

Select CAST(SUM(timediff(timeOut, timeIn)) as time) as totalhours
FROM volHours 
WHERE username = 'skolcz'    
于 2013-02-09T16:36:43.157 回答
1

尝试类似的东西

 Select SUM(timediff(timeOut, timeIn)) as total
 FROM volHours 
 WHERE username = 'skolcz'
于 2013-02-09T16:30:57.103 回答