Sql query to select from 1 hour ago?

SqlMysql

Sql Problem Overview


I have this query but I want to change the date to delete everything that is from more than 1 hour ago based on the server time (or if not possible by server time by post date). How do I do that?

DELETE FROM wp_posts
 WHERE post_date < '2008-06-06 19:18:00' 
   AND post_status = 'publish'

Sql Solutions


Solution 1 - Sql

Use:

DELETE FROM wp_posts
 WHERE post_date < DATE_SUB(NOW(), INTERVAL '1' HOUR)
   AND post_status = 'publish'

Reference:

Solution 2 - Sql

Or even simpler:

SELECT NOW() - INTERVAL 1 HOUR;

So the query becomes:

DELETE FROM wp_posts
 WHERE post_date < NOW() - INTERVAL 1 HOUR
   AND post_status = 'publish'

Solution 3 - Sql

Subdate function works too!

SELECT subdate(current_timestamp, interval 1 hour)

Solution 4 - Sql

DELETE from  table where date_add < NOW() - INTERVAL 1 HOUR;

to see the time run this sql :

select NOW() - INTERVAL 1 HOUR as s1 , NOW() as s2;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - SqlOMG PoniesView Answer on Stackoverflow
Solution 2 - SqlVadym TyemirovView Answer on Stackoverflow
Solution 3 - SqlAminah NurainiView Answer on Stackoverflow
Solution 4 - SqlmehdimmxView Answer on Stackoverflow