Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find records with a date field in the last 24 hours [duplicate]

Tags:

date

sql

mysql

In my SQL query how do i make it find the records in the last 24 hours? Eg

   SELECT * FROM news WHERE date < 24 hours 

I usually do it by setting a variable to date() - 1 day and comparing it to that but I wondered whether the sql query way was faster?

like image 239
user1022585 Avatar asked Nov 10 '11 12:11

user1022585


People also ask

How do I query last 24 hours in SQL?

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 24 hours in MySQL?

MySQL TIME_FORMAT() The time value in MySQL is of the format “HH:MM:SS” and in the 24 hour format.

How can I get data between two timestamps in SQL?

As stated above, the format of date and time in our table shall be yyyy:mm: dd hh:mm: ss which is implied by DATETIME2. The time is in a 24-hour format. Syntax: SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';


1 Answers

You simply select dates that are higher than the current time minus 1 day.

SELECT * FROM news WHERE date >= now() - INTERVAL 1 DAY; 
like image 96
a'r Avatar answered Sep 20 '22 07:09

a'r