使用MySQL查找两个datetime值之间的差异?
要查找两个日期时间值之间的差异,可以使用TIMESTAMPDIFF()
。让我们首先创建一个表-
mysql> create table DemoTable-> (
-> DueDatetime1 datetime,
-> DueDatetime2 datetime
-> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-10-26 19:49:00','2019-10-26 17:49:00');mysql> insert into DemoTable values('2019-10-26 08:00:00','2019-10-26 13:00:00');
mysql> insert into DemoTable values('2019-10-26 06:50:00','2019-10-26 12:50:00');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------------+---------------------+| DueDatetime1 | DueDatetime2 |
+---------------------+---------------------+
| 2019-10-26 19:49:00 | 2019-10-26 17:49:00 |
| 2019-10-26 08:00:00 | 2019-10-26 13:00:00 |
| 2019-10-26 06:50:00 | 2019-10-26 12:50:00 |
+---------------------+---------------------+
3 rows in set (0.00 sec)
这是要实现timestampdiff()
并找到两个日期之间的差异的查询-
mysql> select abs(timestampdiff(minute,DueDatetime1,DueDatetime2)) from DemoTable;
这将产生以下输出-
+------------------------------------------------------+| abs(timestampdiff(minute,DueDatetime1,DueDatetime2)) |
+------------------------------------------------------+
| 120 |
| 300 |
| 360 |
+------------------------------------------------------+
3 rows in set (0.00 sec)
以上是 使用MySQL查找两个datetime值之间的差异? 的全部内容, 来源链接: utcz.com/z/316798.html