mysql查询中的日期时间差的总和

时间:2013-09-27 13:06:51

标签: php mysql

Mysql表列是

Starttime - type:datetime, Stoptime - type:datetime

数据

Starttime, stoptime
2013-10-25 09:00:00, 2013-10-25 17:00:00
2013-10-26 09:00:00, 2013-10-26 17:00:00
2013-10-27 09:00:00, 2013-10-27 17:00:00
2013-10-28 09:00:00, 2013-10-28 17:45:00

查询

select, min(starttime), max(stoptime), 
SUM(TIME_TO_SEC(TIME_DIFF(stoptime - starttime))) total_hours from mytable

会返回一些秒数,我可以使用TIME_TO_SEC()将其转换回来...它会以HH:MM:SS格式生成结果,例如28:45:00

但我需要将28:45:00转换为28.75小时格式。我该怎么做?

2 个答案:

答案 0 :(得分:3)

select, min(starttime), max(stoptime), 
SUM(TIME_TO_SEC(TIME_DIFF(stoptime - starttime))/3600) total_hours from mytable

答案 1 :(得分:0)

SELECT 
MIN(starttime), 
MAX(stoptime), 
SUM(TIME_TO_SEC(TIMEDIFF(stoptime, starttime))/3600) AS total_hours 
FROM mytable