两天的两个时间戳之间的MySql差异?

时间:2010-03-30 14:38:32

标签: mysql datetime timestamp

如何在几天内获得两个时间戳之间的差异?我应该使用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)

7 个答案:

答案 0 :(得分:102)

如果您乐意忽略列中的时间部分,DATEDIFF()会在几天内为您提供所需的差异。

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

答案 1 :(得分:12)

我知道已经很老了,但我只是为了它而说 - 我正在寻找同样的问题而且到了这里,但我需要在几天之内的差异。

我使用了SELECT (UNIX_TIMESTAMP(DATE1) - UNIX_TIMESTAMP(DATE2))/60/60/24 Unix_timestamp以秒为单位返回差异,然后我将其分为分钟(秒/ 60),小时(分钟/ 60),天(小时/ 24)。

答案 2 :(得分:5)

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)

答案 3 :(得分:5)

如果您想以完整的TIMESTAMP格式返回,请尝试: -

 SELECT TIMEDIFF(`call_end_time`, `call_start_time`) as diff from tablename;

返回

     diff
     - - -
    00:05:15

答案 4 :(得分:4)

如果您需要在几天内计算差异,那么

<% if (window.x !== undefined && window.x !== null) { %>
  <div> Foo </div>
<% } %>

它将返回
    SELECT TIMESTAMPDIFF(SECOND,'2010-09-21 21:40:36','2010-10-08 18:23:13')/86400 AS diff
    diff

答案 5 :(得分:3)

SELECT DATEDIFF( now(), '2013-06-20' );

  

这里,datediff有两个参数'upto-date','from-date'

     

我所做的是,使用now()函数,我可以得到否。自2013年6月20日至今的几天。

答案 6 :(得分:0)

选择DATEDIFF(max_date,min_date)作为我桌子的天数。 即使col max_datemin_date位于字符串数据类型中,此方法仍然有效。