我想将列late_am_in
,late_am_out
,late_pm_in
,late_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。
谢谢!
答案 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')))))
;