MySQL 向时间字段添加 12 小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4840833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:35:21  来源:igfitidea点击:

MySQL add 12 hours to a time field

mysql

提问by sdfor

I need to add 12 hours to a MySQL TIMEfield (not DATETIME) and I'm having trouble.

我需要向 MySQLTIME字段添加 12 小时(不是DATETIME),但遇到了问题。

UPDATE `events` 
SET start_time = DATE_ADD(start_time, INTERVAL 12 HOUR)
WHERE `start_time` < '11:00:00'

returns with no errors but doesn't change anything, I think because start_timeis a TIMEfield.

没有错误返回但不会改变任何东西,我认为因为start_time是一个TIME字段。

UPDATE `events` 
SET start_time = start_time + '12:00:00'
WHERE `start_time` < '11:00:00'

adds 12 seconds.

增加 12 秒。

回答by nybbler

Try using ADDTIME instead of DATE_ADD. You could do SET start_time = ADDTIME(start_time, '12:00:00')

尝试使用 ADDTIME 而不是 DATE_ADD。你可以做SET start_time = ADDTIME(start_time, '12:00:00')

回答by Aaron Adams

UPDATE `events` 
SET start_time = start_time + INTERVAL 12 HOUR
WHERE `start_time` < '11:00:00'

The MySQL functions that accept INTERVALarguments are mostly unnecessary; you can just add and subtract intervals with +and -.

接受INTERVAL参数的 MySQL 函数大多是不必要的;你可以只添加和减去具有间隔+-

回答by cuneyt

set start_time = ADDTIME(start_time,'12:00:00')

DATE_ADD works fine with timestamp etc, but not with TIME

DATE_ADD 适用于时间戳等,但不适用于 TIME

回答by Sapna Bhayal

update my_table SET modified_date = ADDTIME(scheduled_date, '03:15:00') 

This will add 3 hours , 15 minutes in modified_date

这将在 modified_date 中增加 3 小时 15 分钟

回答by Bhaskar Bhatt

if the developer does not want to update data and wants to add hours or minutes to time. It can be done following way:

如果开发人员不想更新数据并且想要增加小时或分钟的时间。可以通过以下方式完成:

The developer can use an AddTime() function to add hours to time column in MySQL. It can be used like below way:

开发人员可以使用 AddTime() 函数将小时添加到 MySQL 中的时间列。它可以像下面这样使用:

AddTime(COLUMN,'01:00:00′) to add an hour in MySQL time column
AddTime(COLUMN,'00:01:00′) to add a minute in MySQL time column

sql query example:

sql查询示例:

select id,name,AddTime(login_time,'01:00:00') as login_time FROM `table`