Transform the time differences into seconds using TIME_TO_SEC, add them, and then convert them back : TIME_TO_SEC « Date Time « SQL / MySQL






Transform the time differences into seconds using TIME_TO_SEC, add them, and then convert them back

      
mysql>
mysql> CREATE TABLE mytable (
    ->     id INT AUTO_INCREMENT,
    ->     begintime DATETIME,
    ->     endtime DATETIME,
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO mytable (begintime, endtime) VALUES
    ->         ('2005-03-27 7:15', '2005-03-27 18:00'),
    ->         ('2005-03-28 8:00', '2005-03-28 18:00'),
    ->         ('2005-03-29 7:30', '2005-03-29 16:50'),
    ->         ('2005-03-30 7:00', '2005-03-30 17:15');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(endtime, begintime)))) AS sumtime FROM mytable;
+----------+
| sumtime  |
+----------+
| 40:20:00 |
+----------+
1 row in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Breaking Down Time Intervals into Components
2.If you pass TIME_TO_SEC( ) a date-and-time value, it extracts the time part and discards the date.
3.TIME_TO_SEC( ) converts a TIME value to the equivalent number of seconds
4.TIME_TO_SEC(): strips off the date part and returns the time part as the corresponding number of seconds:
5.To compute the total elapsed time, use TIME_TO_SEC( ) to convert the values to seconds before summing them.
6.Using TIME_TO_SEC( ) to strip off the date part of the t_create values
7.To express time values as minutes, hours, or days, perform the appropriate divisions:
8.Forcing MySQL to Treat Strings as Temporal Values