2013-09-27 211 views
2

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 eg 28:45:00

但我需要將28:45:00轉換爲28.75小時格式。我怎麼做?

+0

試試這個: '選擇,分(開始時間),MAX(停止時間), SUM(TIME_TO_SEC(TIME_DIFF(停止時間 - 開始時間))/ 3600)從mytable' – qisho

+0

大total_hours ..這做的伎倆。如果你張貼作爲答案,我會接受:) – user1421214

回答

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

謝謝 - 我現在不需要使用TIME_TO_SEC()。 – user1421214

+0

TIMEDIFF而不是TIME_DIFF和stoptime,starttime而不是stoptime-starttime – itsazzad

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