MySQL 如何选择时间戳位于当前小时内?
为此,你可以使用 CURTIME()。首先让我们创建一个表 −
mysql> create table DemoTable -> ( -> ArrivalTime timestamp -> ); Query OK, 0 rows affected (0.65 sec)
使用 insert 命令向表中插入一些记录 −
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)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+---------------------+ | 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)
当前时间如下 −
mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 18:43:55 | +-----------+ 1 row in set (0.00 sec)
以下是选择时间戳位于当前小时中的查询 −
mysql> select *from DemoTable where hour(ArrivalTime)=hour(curtime());
这将产生以下输出 −
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-26 18:00:00 | | 2019-10-26 18:55:00 | +---------------------+ 2 rows in set (0.00 sec)
广告