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
mysql adding hours to datetime
提问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 :
和 :
now()
: the current date timedate_sub()
to substract 1 day to that date
now()
: 当前日期时间date_sub()
减去 1 天到该日期
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 Frosty Z
DELETE
FROM table
WHERE datetime_field < DATE_SUB(NOW(), INTERVAL 1 DAY);