get last 1 hour data in mysql

How To Get Last 1 Hour Data in MySQL

It is useful to get last 1 hour records, or retrieve last 1 hour rows for reporting and analysis. In this article, we will look at how to get last 1 hour data in MySQL.


How To Get Last 1 Hour Data in MySQL

It is very easy to select last 1 hour data in MySQL using INTERVAL clause. Let us say you have the following table sales(id, order_date, amount).

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

mysql> insert into sales(id, order_date, amount)
      values(1, '2021-02-02 08:15:00',250),
      (2, '2021-02-02 08:30:00',150),
      (3, '2021-02-02 08:55:00',200),
      (4, '2021-02-02 09:15:00',125),
      (5, '2021-02-02 09:30:00',200),
      (6, '2021-02-02 09:45:00',250);

 mysql> select * from sales;
 +------+---------------------+--------+
 | id   | order_date          | amount |
 +------+---------------------+--------+
 |    1 | 2021-02-02 08:15:00 |    250 |
 |    2 | 2021-02-02 08:30:00 |    150 |
 |    3 | 2021-02-02 08:55:00 |    200 |
 |    4 | 2021-02-02 09:15:00 |    125 |
 |    5 | 2021-02-02 09:30:00 |    200 |
 |    6 | 2021-02-02 09:45:00 |    250 |
 +------+---------------------+--------+

Also read : How to show rows not present in another table in MySQL

Here is the SQL to show latest time using now() function.

mysql> select now();
 +---------------------+
 | now()               |
 +---------------------+
 | 2021-02-02 09:48:27 |
 +---------------------+

Here is the SQL to get last 1 hour data in MySQL.

mysql> select * 
       from sales 
       where order_date > now() - interval 1 hour;
 +------+---------------------+--------+
 | id   | order_date          | amount |
 +------+---------------------+--------+
 |    3 | 2021-02-02 08:55:00 |    200 |
 |    4 | 2021-02-02 09:15:00 |    125 |
 |    5 | 2021-02-02 09:30:00 |    200 |
 |    6 | 2021-02-02 09:45:00 |    250 |
 +------+---------------------+--------+

Also read : How to Get Current Week 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.


How to Get Current Hour Data in MySQL

Sometimes you may need to get records of current hour in MySQL. Here is the SQL query to retrieve current hour data.

mysql> select * from sales 
       where date(order_date)=date(now()) 
        and hour(order_date)=hour(now());
 +------+---------------------+--------+
 | id   | order_date          | amount |
 +------+---------------------+--------+
 |    4 | 2021-02-02 09:15:00 |    250 |
 |    5 | 2021-02-02 09:30:00 |    250 |
 |    6 | 2021-02-02 09:45:00 |    250 |
 +------+---------------------+--------+

In the above SQL query, we select only those records whose order_date column’s DATE and HOUR values are same as those of present datetime. We use DATE and HOUR functions to get date and hour values from datetime values.

Need a reporting tool for MySQL? Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!