为此,请使用TIMESTAMPDIFF()
。让我们创建一个表-
mysql> create table demo42 −> ( −> start_date datetime −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo42 values('2020-01-10 12:30:05'); mysql> insert into demo42 values('2019-02-24 10:40:45'); mysql> insert into demo42 values('2020-05-12 05:45:55'); mysql> insert into demo42 values('2020-05-12 05:40:55'); mysql> insert into demo42 values('2020-05-12 05:42:55');
使用select语句显示表中的记录-
mysql> select *from demo42;
这将产生以下输出-
+---------------------+ | start_date | +---------------------+ | 2020-01-10 12:30:05 | | 2019-02-24 10:40:45 | | 2020-05-12 05:45:55 | | 2020-05-12 05:40:55 | | 2020-05-12 05:42:55 | +---------------------+ 5 rows in set (0.00 sec)
以下是使用TIMESTAMPDIFF选择行的查询-
mysql> select *from demo42 −> where ABS(TIMESTAMPDIFF(MINUTE,start_date, '2020−05−12 05:40:55')) < 5;
这将产生以下输出-
+---------------------+ | start_date | +---------------------+ | 2020−05−12 05:40:55 | | 2020−05−12 05:42:55 | +---------------------+ 2 rows in set (0.00 sec)