MySQL - How to SUM times?

前端 未结 5 2040
礼貌的吻别
礼貌的吻别 2020-11-29 12:15

I have a table that contains date-time values in this format:

START

1/13/2009 7:00:00AM

END

相关标签:
5条回答
  • 2020-11-29 12:45
    SELECT some_grouping_column, SUM(TIMEDIFF(datecol1,datecol2)) as tot_time
    FROM my_table
    GROUP BY some_grouping_column;
    
    0 讨论(0)
  • 2020-11-29 12:46

    I think you have to use this code... it works :)

    SELECT  
      SEC_TO_TIME( SUM( TIME_TO_SEC( `time` ) ) ) AS total_time  
    FROM time_table;
    
    0 讨论(0)
  • 2020-11-29 12:46

    Check out the MySQL DateDiff Function

    SELECT SUM(DATEDIFF(endtime, starttime)) 
    FROM somerecordstable
    WHERE starttime > '[some date]' AND endtime <= '[some date]'
    

    You could also try:

    SELECT SUM(TIMEDIFF(endtime, starttime)) 
    FROM somerecordstable
    WHERE starttime > '[some date]' AND endtime <= '[some date]'
    

    I haven't tested the second one, but I know the first one should work.

    0 讨论(0)
  • 2020-11-29 12:54

    Try looking into UNIX_TIMESTAMP and SEC_TO_TIME.

    You would sum up the differences between the timestamps, then use that value (would be in milliseconds) to get the time:

    SELECT SEC_TO_TIME(time_milis / 1000)
    FROM (
       SELECT SUM(UNIX_TIMESTAMP(date1) - UNIX_TIMESTAMP(date2)) as time_milis
       FROM table
    )
    
    0 讨论(0)
  • 2020-11-29 13:11

    Use TIMEDIFF().

    SUM(TIMEDIFF(end, start))
    
    0 讨论(0)
提交回复
热议问题