sql - 如何计算MySQL中日期时间之间的间隔?

标签 sql mysql

例如,如何计算这两个日期时间之间的间隔:

2009-09-18 00:00:00

2009-10-17 00:00:00

编辑

我的意思是获取格式为年-月-日时:分:秒的区间

最佳答案

使用 datediff 怎么样? :

mysql> select abs(datediff('2009-09-18 00:00:00', '2009-10-17 00:00:00'));
+-------------------------------------------------------------+
| abs(datediff('2009-09-18 00:00:00', '2009-10-17 00:00:00')) |
+-------------------------------------------------------------+
|                                                          29 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

引用手册:

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions.
Only the date parts of the values are used in the calculation.

关于sql - 如何计算MySQL中日期时间之间的间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1458345/

相关文章:

php - 每月 echo 分数,即使该月没有数据

mysql - MySQL 中缺少 InnoDB

php - 如何使用limit检查mysql中的数据?

sql - 从具有相同名称但不同值的sql行中获取值

php - 向Mysql数据库中插入数据时出错

php - XMLHttpRequest 中的 UTF-8 字符已损坏

C# MySQL EF 6 代码优先迁移使用动态 where 子句准备语句创建自定义存储过程给出错误

sql - 如何在 Sybase Anywhere 11 中更改数据库用户名和数据库组名

sql - 没有sql server的delphi数据库

mysql - 检查两个表中是否存在一行