MySQL-一组时差之和 [英] MySQL - SUM of a group of time differences

查看:71
本文介绍了MySQL-一组时差之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to sum all the time differences to show the total hours worked by a volunteer. Getting a result set of time differences is easy:

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

它提供了按小时列出的时间列表,但是我想将其总计为一个总计.

which gives the list of times by hours but then I want to sum it up to a grand total.

因此,如果结果集为:

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

总共需要40个小时.

这有一种做类似的事情的方式:

This there a way to do something like:

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

?

推荐答案

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

这篇关于MySQL-一组时差之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆