calculate a sum of type time using sql

前端 未结 4 780
孤城傲影
孤城傲影 2020-11-27 06:34

How to caculate sum of times of my colonne called \"timeSpent\" having this format: HH:mm in SQL? I am using MySQL.

the type of my column is Time.

it has th

4条回答
  •  感情败类
    2020-11-27 06:54

    In MySQL, you would do something like this to get the time interval:

    SELECT TIMEDIFF('08:00:00', '10:00:00');
    

    Then to add the time intervals, you would do:

    SELECT ADDTIME('01:00:00', '01:30:00');
    

    Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.

    Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():

    UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
    SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
    

提交回复
热议问题