Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating total time duration in MySQL

Tags:

sql

mysql

i have a table data like this

(int)   (time)  [mySQL datatype]

Id     Duration 
------------------
1      00:10:00
2      10:10:00
3      03:00:00
4      04:13:00

i want to calculate the total duration from this table, how can we do it. like 17:33:00. Could anybody please provide mysql query to calculate this.

like image 833
A.P.S Avatar asked Jul 31 '10 11:07

A.P.S


People also ask

How does MySQL calculate duration?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR . To get the difference in seconds as we have done here, choose SECOND .

How does MySQL calculate datetime difference?

MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.


1 Answers

Try converting to seconds, summing, then converting back to a time:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(Duration)))
FROM Table1

Result:

17:33:00
like image 51
Mark Byers Avatar answered Sep 30 '22 02:09

Mark Byers