How To Get Records from Last 24 Hours in MySQL

Sometimes you may need to get last 24 hours data or select records for last 24 hours, for reporting & analysis. Here’s how to get records from last 24 hours in MySQL. You can use this SQL query to get rows for last 24 hours in your database.

 

How To Get Records from Last 24 Hours in MySQL

Here is the SQL query to get records from last 24 hours in MySQL.

Let’s say you have the following table sales(order_date, amount)

mysql> create table sales(order_date datetime, amount int);

mysql> insert into sales(order_date,amount) 
     values('2020-06-07 01:00:00',200),
     ('2020-06-07 02:30:00',350),
     ('2020-06-07 04:40:00',410),
     ('2020-06-07 12:10:00',600),
     ('2020-06-07 15:00:00',300),
     ('2020-06-07 18:55:00',450),
     ('2020-06-07 21:00:00',1200),
     ('2020-06-08 03:00:00',800),
     ('2020-06-08 05:30:00',900),
     ('2020-06-08 07:20:00',100),
     ('2020-06-08 10:10:00',250),
     ('2020-06-08 12:05:00',300),
     ('2020-06-08 13:30:00',200);

mysql> select * from sales;
+---------------------+--------+
| order_date          | amount |
+---------------------+--------+
| 2020-06-07 01:00:00 |    200 |
| 2020-06-07 02:30:00 |    350 |
| 2020-06-07 04:40:00 |    410 |
| 2020-06-07 12:10:00 |    600 |
| 2020-06-07 15:00:00 |    300 |
| 2020-06-07 18:55:00 |    450 |
| 2020-06-07 21:00:00 |   1200 |
| 2020-06-08 03:00:00 |    800 |
| 2020-06-08 05:30:00 |    900 |
| 2020-06-08 07:20:00 |    100 |
| 2020-06-08 10:10:00 |    250 |
| 2020-06-08 12:05:00 |    300 |
| 2020-06-08 13:30:00 |    200 |
+---------------------+--------+

Bonus Read : How to Get First Record in Each Group in MySQL

 

Get Records from Last 24 Hours in MySQL

Here’s the SQL query to get records from last 24 hours in MySQL

mysql> select * from sales
       where order_date > now() - interval 24 hour;
+---------------------+--------+
| order_date          | amount |
+---------------------+--------+
| 2020-06-07 15:00:00 |    300 |
| 2020-06-07 18:55:00 |    450 |
| 2020-06-07 21:00:00 |   1200 |
| 2020-06-08 03:00:00 |    800 |
| 2020-06-08 05:30:00 |    900 |
| 2020-06-08 07:20:00 |    100 |
| 2020-06-08 10:10:00 |    250 |
| 2020-06-08 12:05:00 |    300 |
| 2020-06-08 13:30:00 |    200 |
+---------------------+--------+

In the above SQL query, we use MySQL system function now() to get current datetime. Then we use INTERVAL clause to select those rows where order_date falls within past 24 hours of present datetime.

Bonus Read : How to Get Last 15 Days Records in MySQL

 

Instead of specifying interval in hours, you can also mention it in day.

mysql> select * from sales
     where order_date > now() - interval 1 day;
+---------------------+--------+
| order_date          | amount |
+---------------------+--------+
| 2020-06-07 15:00:00 |    300 |
| 2020-06-07 18:55:00 |    450 |
| 2020-06-07 21:00:00 |   1200 |
| 2020-06-08 03:00:00 |    800 |
| 2020-06-08 05:30:00 |    900 |
| 2020-06-08 07:20:00 |    100 |
| 2020-06-08 10:10:00 |    250 |
| 2020-06-08 12:05:00 |    300 |
| 2020-06-08 13:30:00 |    200 |
+---------------------+--------+

Both the above queries will help you get records from last 24 hours.

Bonus Read : How to Get Last Record in Each Group in MySQL

 

However, if you want to select rows for present day only, you can use the following query.

mysql> select * from sales
      where date(order_date) = date(now());
+---------------------+--------+
| order_date          | amount |
+---------------------+--------+
| 2020-06-08 03:00:00 |    800 |
| 2020-06-08 05:30:00 |    900 |
| 2020-06-08 07:20:00 |    100 |
| 2020-06-08 10:10:00 |    250 |
| 2020-06-08 12:05:00 |    300 |
| 2020-06-08 13:30:00 |    200 |
+---------------------+--------+

In the above query, you will get rows only present date, and not past 24 hours. We use DATE function to select only those rows where the date value of order_date is same as date value of now() function, that is, present date.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!