MySQL Select where timestamp is in the current hour?



For this, you can use CURTIME(). Let us first create a table −

mysql> create table DemoTable    -> (    -> ArrivalTime timestamp    -> ); Query OK, 0 rows affected (0.65 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-10-26 17:55:55'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2019-10-26 18:00:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-10-26 18:55:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-10-26 16:00:10'); Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+ | ArrivalTime         | +---------------------+ | 2019-10-26 17:55:55 | | 2019-10-26 18:00:00 | | 2019-10-26 18:55:00 | | 2018-10-26 16:00:10 | +---------------------+ 4 rows in set (0.00 sec)

The current time is as follows −

mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 18:43:55  | +-----------+ 1 row in set (0.00 sec)

Following is the query to select where the timestamp is in the current hour −

mysql> select *from DemoTable where hour(ArrivalTime)=hour(curtime());

This will produce the following output −

+---------------------+ | ArrivalTime         | +---------------------+ | 2019-10-26 18:00:00 | | 2019-10-26 18:55:00 | +---------------------+ 2 rows in set (0.00 sec)
Updated on: 2019-12-12T07:15:06+05:30

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements