mysql如何在列中加总时间

时间:2017-05-25 07:55:16

标签: mysql

我想将列late_am_inlate_am_outlate_pm_inlate_pm_out和结果汇总到total_hours 列。

    SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(late_pm_in, '%H:%I') - TIME_TO_SEC(STR_TO_DATE
(late_pm_out, '%H:%I')))) AS total_hours

我的列的数据类型是varchar。

谢谢!

sum time in mysql

我想达到这样的目标: want to achieve

1 个答案:

答案 0 :(得分:0)

尝试这样做有效。

SEC_TO_TIME((TIME_TO_SEC(STR_TO_DATE(late_am_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_am_in, '%H:%i'))) + (TIME_TO_SEC(STR_TO_DATE(late_pm_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_pm_in, '%H:%i')))) AS total_hours

以下是如何更新表格以计算总小时数

    Update Test SET total_hours = SEC_TO_TIME((TIME_TO_SEC(STR_TO_DATE(late_am_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_am_in, '%H:%i'))) + (TIME_TO_SEC(STR_TO_DATE(late_pm_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_pm_in, '%H:%i'))))

下面是如何计算插入值时的总小时数

INSERT INTO Test
    (`id`, `late_am_in`, `late_am_out`, `late_pm_in`, `late_pm_out`, `total_hours`)
VALUES
    (7, '14:15', '16:15', '18:15', '19:19', SEC_TO_TIME((TIME_TO_SEC(STR_TO_DATE(late_am_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_am_in, '%H:%i'))) + (TIME_TO_SEC(STR_TO_DATE(late_pm_out, '%H:%i')) -  TIME_TO_SEC(STR_TO_DATE(late_pm_in, '%H:%i')))))
;