让我们首先创建一个表-
mysql> create table DemoTable1570 -> ( -> ArrivalTime datetime -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1570 values('2019-10-15 5:10:00'); mysql> insert into DemoTable1570 values('2019-10-15 23:00:00'); mysql> insert into DemoTable1570 values('2019-10-15 23:10:00'); mysql> insert into DemoTable1570 values('2019-10-15 16:10:00');
使用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)