DATETIME select rows from last 2 hours

You can use simpler notation:

SELECT * FROM Posts WHERE `Date` > NOW() - INTERVAL 2 HOUR

change to this:

SELECT * FROM Posts WHERE `Date` > SUBDATE( CURRENT_TIMESTAMP, INTERVAL 2 HOUR)

That's because you're using CURRENT_DATE, you should use NOW() or CURRENT_TIMESTAMP instead.

The problem is that using CURRENT_DATE, being a date value, the time defaults to 00:00:00, so by substracting 2 hours you end up getting 22:00:00 of the previous day, instead of the last 2 hours...


You can try this one

SELECT * FROM Posts WHERE `Date` > DATE_SUB(CURDATE(), INTERVAL 3 HOUR)

Tags:

Mysql

Sql