I'm trying to select rows from the last 2 hours. For some reason it doesn't work. The code syntax looks ok and works when used on other tables, but for some reason when used on table Posts
, get rows a lot older than 2 hours.
SELECT * FROM Posts WHERE `Date` > SUBDATE( CURRENT_DATE, INTERVAL 2 HOUR)
Is there any problem with the code? Is there another way to write it? What could be the reason for this?
You can use date-sub() and now() function from MySQL to fetch the rows added in last hour.
If you want to select the last 24 hours from a datetime field, substitute 'curate()' with 'now()'. This also includes the time.
Here is the SQL to show latest time using now() function. Here is the SQL to get last 1 hour data in MySQL. In the above query, we select only those rows whose order_date falls within past 1 hour interval. We use INTERVAL clause to easily substract 1 hour interval from present time obtained using now() function.
MySQL INTERVAL() function returns the index of the argument that is more than the first argument. It returns 0 if 1st number is less than the 2nd number and 1 if 1st number is less than the 3rd number and so on or -1 if 1st number is NULL.
You can use simpler notation:
SELECT * FROM Posts WHERE `Date` > NOW() - INTERVAL 2 HOUR
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With