2016-11-19 314 views
0

如何計算存儲在MySQL列中的時間總和? 表名:時間表如何獲得mysql的時間總和?

+----------+ 
| hours | 
+----------+ 
| 02:00:00 | 
| 03:30:00 | 
| 04:30:00 | 
| 05:30:00 | 
| 06:00:00 | 
| 07:00:00 | 
| 08:00:00 | 
| 09:00:00 | 
+----------+ 

我需要輸出:45:30:00。 我試着用下面的SQL

select sum(hours) from timesheet ; 

卻是露出449000 能否請您通過適當的SQL指正?

+0

請檢查這個問題,這可能是幫助你[Sackoverflow問題(http://stackoverflow.com/questions/3054123/calculate-a-sum-of -type-time-using-sql) – Apoorva

+0

'select SEC_TO_TIME(sum(TIME_TO_SEC(hours)))from timeheet;' – Damith

+0

Yah !!它正在工作。非常感謝你@Damith。 – Mihir

回答

2

您可以使用SEC_TO_TIME和TIME_TO_SEC date time functions

select SEC_TO_TIME(sum(TIME_TO_SEC(hours))) from timesheet ;