Advertisement
Advertisement


How to select records from last 24 hours using SQL?


Question

I am looking for a where clause that can be used to retrieve records for the last 24 hours?

2013/11/07
1
186
11/7/2013 4:08:26 PM

Accepted Answer

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
2009/12/11
101
12/11/2009 2:54:40 PM


MySQL :

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)

The INTERVAL can be in YEAR, MONTH, DAY, HOUR, MINUTE, SECOND

For example, In the last 10 minutes

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
2013/02/05

Which SQL was not specified, SQL 2005 / 2008

SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())

If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.

2009/12/11

in postgres, assuming your field type is a timestamp:

select * from table where date_field > (now() - interval '24 hour');

2012/06/20

If the timestamp considered is a UNIX timestamp You need to first convert UNIX timestamp (e.g 1462567865) to mysql timestamp or data

SELECT * FROM `orders` WHERE FROM_UNIXTIME(order_ts) > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
2016/05/06

select ...
from ...
where YourDateColumn >= getdate()-1
2009/12/11

Source: https://stackoverflow.com/questions/1888544
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]