Pull MySQL records for the last 60 minutes?


To pull records for the last 60 minutes, use MySQL INTERVAL as shown in the below syntax −

select *from yourTableName where yourColumnName > now() - interval 60 minute;

Let us first create a table −

mysql> create table DemoTable
(
   ArrivalTime datetime
);
Query OK, 0 rows affected (0.61 sec)

Let us find the current date −

mysql> select now();
+-----------------------+
| now()                 |
+-----------------------+
| 2019-09-17 00 :04 :54 |
+-----------------------+
1 row in set (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-09-16 08 :00 :00');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('2019-09-17 11 :00 :00');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('2019-09-12 12 :00 :00');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------------------+
| ArrivalTime           |
+-----------------------+
| 2019-09-16 08 :00 :00 |
| 2019-09-17 11 :00 :00 |
| 2019-09-12 12 :00 :00 |
+-----------------------+
3 rows in set (0.00 sec)

Following is the query to pull record for the last 60 minutes −

mysql> select *from DemoTable where ArrivalTime > now() - interval 60 minute;

This will produce the following output −

+-----------------------+
| ArrivalTime           |
+-----------------------+
| 2019-09-17 11 :00 :00 |
+-----------------------+
1 row in set (0.00 sec)
raja
Published on 09-Oct-2019 14:19:14
Advertisements