小编典典

Mysql在两个时间戳之间的天数差异?

mysql

如何获得几天内两个时间戳之间的差异?我应该为此使用datetime列吗?


我将专栏切换为日期时间。简单的减法似乎并没有在几天内给我带来结果。

mysql> SELECT NOW(), last_confirmation_attempt, NOW() - last_confirmation_attempt AS diff  FROM DateClubs HAVING diff IS NOT NULL ;
+---------------------+---------------------------+-----------------+
| NOW()               | last_confirmation_attempt | diff            |
+---------------------+---------------------------+-----------------+
| 2010-03-30 10:52:31 | 2010-03-16 10:41:47       | 14001084.000000 |
+---------------------+---------------------------+-----------------+
1 row in set (0.00 sec)

我不认为diff是几秒钟,因为当我将diff一天中的秒数除以(86,400)时,我不会得到一个明智的答案:

mysql> SELECT NOW(), last_confirmation_attempt, ( NOW() - last_confirmation_attempt) / 86400 AS diff  FROM DateClubs HAVING diff IS NOT NULL ;
+---------------------+---------------------------+----------------+
| NOW()               | last_confirmation_attempt | diff           |
+---------------------+---------------------------+----------------+
| 2010-03-30 10:58:58 | 2010-03-16 10:41:47       | 162.0568402778 |
+---------------------+---------------------------+----------------+
1 row in set (0.00 sec)

阅读 607

收藏
2020-05-17

共1个答案

小编典典

如果您乐于忽略列中的时间部分,则DATEDIFF()会为您提供以天为单位的时差。

SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;
+------+
| days |
+------+
|   17 |
+------+
2020-05-17