mysql 将小时添加到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5418560/
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 19:16:48  来源:igfitidea点击:

mysql adding hours to datetime

mysqldatetime

提问by luca

In MYSQL DB I need to check if a "datetime" field is more than 24hours (or whatever) ago in which case delete the row.

在 MYSQL DB 中,我需要检查“日期时间”字段是否超过 24 小时(或其他),在这种情况下删除该行。

How to add hours to datetime in mysql?

如何在mysql中为日期时间添加小时数?

thanks

谢谢

Luca

卢卡

回答by Pascal MARTIN

What about something like this :

这样的事情怎么样:

delete
from your_table
where your_field <= date_sub(now(), INTERVAL 1 day)


With :


和 :


Or, if you want o use 24 hours instead of 1 day :


或者,如果您想使用 24 小时而不是 1 天:

delete
from your_table
where your_field <= date_sub(now(), INTERVAL 24 hour)

回答by Alin Purcaru

You have the Date and Time functions.

您有日期和时间功能

WHERE `yourDate` < DATE_SUB(NOW(),INTERVAL 1 DAY)

or shorter

或更短

WHERE `yourDate` < NOW() - INTERVAL 1 DAY

回答by Muhamad Bhaa Asfour

there is the addtime()method in mysql

mysql 中有addtime()方法

回答by Frosty Z

DELETE 
FROM table 
WHERE datetime_field < DATE_SUB(NOW(), INTERVAL 1 DAY);