31

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

How to add hours to datetime in mysql?

thanks

Luca

4 Answers 4

44

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 :

delete
from your_table
where your_field <= date_sub(now(), INTERVAL 24 hour)
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldnt this be date_add, because your not using -1 day? date_sub does the minus, date_add does the plus.
13

You have the Date and Time functions.

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

or shorter

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

1 Comment

Thanks. This is surprisingly working: SELECT '2018-05-31' + INTERVAL 1 DAY;
3

there is the addtime() method in mysql

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.