I am trying to select post which are added on last hour from current time. I have explored previous questions but they are using fixed date or datetime. To achieve this, I am using following query:
SELECT * FROM `wp_posts`
WHERE post_date >= SUBTIME(CURRENT_TIMESTAMP(), "1:0:0")
and post_date <= CURRENT_TIMESTAMP();
This query is not displaying any result. When when I am using it without AND it just working perfect. So, How to select previous hour records using current datimetime as I am using CURRENT_TIMESTAMP() for this?
2
Answers
Query:
Output:
fiddle
I have tested your code, and it works fine for me. If no row is displayed, it means that there is no post_date between CURRENT_TIMESTAMP() and 1 hour ago.
On the other hand, if we assume that post_date is always going to be less than your CURRENT_TIMESTAMP()( i do not know if it is your case; you didn’t specify), as a suggestion, you could improve your code by just simply doing this.
If post_date is always less than CURRENT_TIMESTAMP(), there is no need to include it.