我有两次24小时格式:
| JamProduksi| JamKerja |
|------------|----------|
| 22:00:00 |05:00:00 |
我希望结果是07:00:00
我在mysql中使用timediff,但是当JamProduksi高于JamKerja时,它会给我一个结果 - > -17:00:00
你可以帮帮我们吗?答案 0 :(得分:0)
两个选项:
TIMEDIFF()
; SUBTIME()
。答案 1 :(得分:0)
使用case
检查哪一个更大:
select
(
case
when JamProduksi > JamKerja
then TIMEDIFF('24:0:0',TIMEDIFF(JamProduksi, JamKerja))
when JamProduksi < JamKerja
then TIMEDIFF('24:0:0',TIMEDIFF(JamKerja, JamProduksi))
end
)
答案 2 :(得分:0)
假设没有人工作时间超过24小时,您可以调整结果以适应一天。
CREATE TABLE `production_working_times` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`JamProduksi` time DEFAULT NULL,
`JamKerja` time DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `production_working_times` (`id`, `JamProduksi`, `JamKerja`)
VALUES
(1, '22:00:00', '05:00:00'),
(2, '05:00:00', '22:00:00');
SELECT
JamProduksi started_work,
JamKerja finished_work,
-- convert back to time
sec_to_time(
-- remove any time beyond 24 hours
mod(
-- convert the time seconds and add the number of seconds in one day
time_to_sec(
-- get the time difference
timediff(JamKerja,JamProduksi)
) + 86400
,86400
)
) hours_worked
FROM production_working_times
-- sample results
started_work finished_work hours_worked
22:00:00 05:00:00 07:00:00
05:00:00 22:00:00 17:00:00