将时间(hh:mm:ss)添加到SQL中的datetime字段

时间:2017-05-05 11:54:05

标签: mysql sql datetime

我想将run_duration列(hh:mm:ss)添加到run_datetime(datetime)列,以使用以下查询计算结束时间:

SELECT checkdate, run_datetime, run_duration,
cast(run_datetime as datetime) + cast(run_duration as datetime) as readytime, 
cast(cast(run_datetime as datetime) + cast(run_duration as datetime) as datetime) as readytime_datetime
FROM table

问题在于它没有为某些记录正确添加,因为它总计超过60秒:

enter image description here

还有其他方法可以正确地执行此操作吗?

1 个答案:

答案 0 :(得分:2)

使用MySQL内置的addtime()函数代替+运算符:

SELECT checkdate,
       run_datetime,
       run_duration,
       addtime(cast(run_datetime as datetime), cast(run_duration as time)) as readytime
FROM table

+运算符将日期时间转换为数字并将其相加。