MySql中两个时间戳的区别在几天?
我怎样才能获得两天时间戳之间的差异? 我应该使用这个date时间列吗?
我把我的专栏切换到date时间。 简单的减法似乎没有在几天内给我一个结果。
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
是在几秒钟内,因为当我在一天(86,400)的秒数除diff
,我没有得到一个合理的答案:
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)
如果您很乐意忽略列中的时间部分, DATEDIFF()会在几天内为您提供所需的差异。
SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days; +------+ | days | +------+ | 17 | +------+
我知道已经很老了,但是我只是为了说 – 我正在寻找同样的问题,到了这里,但是我需要几天的时间。
我使用了SELECT (UNIX_TIMESTAMP(DATE1) - UNIX_TIMESTAMP(DATE2))/60/60/24
Unix_timestamp以秒为单位返回差异,然后我分为几分钟(秒/ 60),几小时(分钟/ 60),几天/ 24)。
如果您想以完整的TIMESTAMP格式返回,请尝试下列操作: –
SELECT TIMEDIFF(`call_end_time`, `call_start_time`) as diff from tablename;
回报如
diff - - - 00:05:15
CREATE TABLE t (d1 timestamp, d2 timestamp); INSERT INTO t VALUES ('2010-03-11 12:00:00', '2010-03-30 05:00:00'); INSERT INTO t VALUES ('2010-03-11 12:00:00', '2010-03-30 13:00:00'); INSERT INTO t VALUES ('2010-03-11 00:00:00', '2010-03-30 13:00:00'); INSERT INTO t VALUES ('2010-03-10 12:00:00', '2010-03-30 13:00:00'); INSERT INTO t VALUES ('2010-03-10 12:00:00', '2010-04-01 13:00:00'); SELECT d2, d1, DATEDIFF(d2, d1) AS diff FROM t; +---------------------+---------------------+------+ | d2 | d1 | diff | +---------------------+---------------------+------+ | 2010-03-30 05:00:00 | 2010-03-11 12:00:00 | 19 | | 2010-03-30 13:00:00 | 2010-03-11 12:00:00 | 19 | | 2010-03-30 13:00:00 | 2010-03-11 00:00:00 | 19 | | 2010-03-30 13:00:00 | 2010-03-10 12:00:00 | 20 | | 2010-04-01 13:00:00 | 2010-03-10 12:00:00 | 22 | +---------------------+---------------------+------+ 5 rows in set (0.00 sec)
SELECT DATEDIFF( now(), '2013-06-20' );
这里过时了两个论点“最新”,“从最新”
我所做的是使用now()函数,我不能。 自2013年6月20日至今天。
如果你需要天数的差异达到第二个:
SELECT TIMESTAMPDIFF(SECOND,'2010-09-21 21:40:36','2010-10-08 18:23:13')/ 86400 AS diff
将返回16.8629天。