用于检查如何获取时间差的 MySQL 查询
首先,让我们创建一个表 -
mysql> create table DemoTable1570 -> ( -> ArrivalTime datetime -> ); Query OK, 0 rows affected (0.87 sec)
使用 insert 命令在表内插入一些记录 -
mysql> insert into DemoTable1570 values('2019-10-15 5:10:00'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:00:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1570 values('2019-10-15 16:10:00'); Query OK, 1 row affected (0.15 sec)
使用 select 语句,从表格中显示所有记录 -
mysql> select * from DemoTable1570;
这会生成以下输出
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-15 05:10:00 | | 2019-10-15 23:00:00 | | 2019-10-15 23:10:00 | | 2019-10-15 16:10:00 | +---------------------+ 4 rows in set (0.00 sec)
当前日期如下 -
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-10-15 22:26:14 | +---------------------+ 1 row in set (0.00 sec)
以下是用于获取时间差的查询 -
mysql> select * from DemoTable1570 -> where ArrivalTime > now() - interval 5 hour;
这会生成以下输出
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-15 23:00:00 | | 2019-10-15 23:10:00 | +---------------------+ 2 rows in set (0.00 sec)
广告