要提取最近60分钟的记录,请使用MySQL INTERVAL,如以下语法所示-
select *from yourTableName where yourColumnName > now() - interval 60 minute;
让我们首先创建一个表-
mysql> create table DemoTable ( ArrivalTime datetime );
让我们找到当前日期-
mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-09-17 00 :04 :54 | +-----------------------+ 1 row in set (0.00 sec)
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-09-16 08 :00 :00'); mysql> insert into DemoTable values('2019-09-17 11 :00 :00'); mysql> insert into DemoTable values('2019-09-12 12 :00 :00');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------------------+ | 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)
以下是查询以提取最近60分钟的记录-
mysql> select *from DemoTable where ArrivalTime > now() - interval 60 minute;
这将产生以下输出-
+-----------------------+ | ArrivalTime | +-----------------------+ | 2019-09-17 11 :00 :00 | +-----------------------+ 1 row in set (0.00 sec)