Last updated on June 15th, 2020 at 06:51 am
Many times you may need to get rows from last 10 minutes or get last 10 minutes data in MySQL. You will need to get records from last 10 minutes in MySQL using SQL query, since there is no built-in function for it. Here’s the SQL to select records for last 10 minutes.
How to Get Records from Last 10 Minutes
Here’s the SQL to get records from last 10 minutes. Let’s say you have the following table orders(order_date, amount) that contains a list of all orders.
mysql> create table orders(order_date datetime,amount int); mysql> insert into orders(order_date,amount) values('2020-06-12 08:40:00',235), ('2020-06-12 08:45:00',215), ('2020-06-12 08:47:00',225), ('2020-06-12 08:48:00',135), ('2020-06-12 08:50:00',235), ('2020-06-12 08:52:00',265), ('2020-06-12 08:55:00',205), ('2020-06-12 08:57:00',285); mysql> select * from orders; +---------------------+--------+ | order_date | amount | +---------------------+--------+ | 2020-06-12 08:40:00 | 235 | | 2020-06-12 08:45:00 | 215 | | 2020-06-12 08:47:00 | 225 | | 2020-06-12 08:48:00 | 135 | | 2020-06-12 08:50:00 | 235 | | 2020-06-12 08:52:00 | 265 | | 2020-06-12 08:55:00 | 205 | | 2020-06-12 08:57:00 | 285 | +---------------------+--------+
Bonus Read : How to Get records from Last 7 Days in MySQL
How to get records from last 10 minutes in MySQL
Here’s the SQL query to select records for last 10 minutes.
mysql> select * from orders where order_date > now() - interval 10 minute; +---------------------+--------+ | order_date | amount | +---------------------+--------+ | 2020-06-12 08:55:00 | 205 | | 2020-06-12 08:57:00 | 285 | +---------------------+--------+
Bonus Read : How to Get Records from Last 30 days
In the above query we select those records where order_date falls after a past interval of 10 minutes. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 10 minutes in the past.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.