Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATETIME select rows from last 2 hours

Tags:

sql

mysql

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?

like image 457
lisovaccaro Avatar asked Nov 19 '12 22:11

lisovaccaro


People also ask

How do I get last two hours of data in SQL?

You can use date-sub() and now() function from MySQL to fetch the rows added in last hour.

How do I select records from the last 24 hours in SQL Server?

If you want to select the last 24 hours from a datetime field, substitute 'curate()' with 'now()'. This also includes the time.

How do I get last one hour data in SQL?

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.

What does interval do in SQL?

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.


1 Answers

You can use simpler notation:

SELECT * FROM Posts WHERE `Date` > NOW() - INTERVAL 2 HOUR
like image 160
Ray Avatar answered Sep 30 '22 19:09

Ray