使用时间戳记差异获取错误的值

时间:2019-02-22 15:19:52

标签: mysql

我有2个字段是DAYTIME类型,当我使用函数timestampdiff(t1,t2)时,它返回错误的值,下面是示例:

timestampdiff(SECOND, t.wfStartTimestamp, t.wfEndTimeStamp) 

(这两个字段实际上是白天)

在应返回15时返回89734,有什么建议吗?

1 个答案:

答案 0 :(得分:0)

正在比较的DATETIME值不是您认为正在比较的DATETIME值。这是一个建议...

运行这样的查询:

SELECT t.wfstarttimestamp 
     , t.wfendtimestamp
     , TIMESTAMPDIFF(SECOND,t.wfstarttimestamp,t.wfendtimestamp) AS secs
  FROM t
 WHERE ...
 ORDER
    BY ...
 LIMIT ... 

然后发布结果。我们可以添加演示,以证明内置TIMESTAMPDIFF函数可按文档操作。除非有人重写或修改了内置的TIMESTAMPDIFF函数,否则问题就出在这里。


SELECT t.wfstarttimestamp
     , t.wfendtimestamp
     , TIMESTAMPDIFF(SECOND,t.wfstarttimestamp,t.wfendtimestamp) AS secs
  FROM (
         SELECT '2019-02-22 10:20:30' + INTERVAL 0 SECOND AS wfstarttimestamp,  '2019-02-22 10:20:45' + INTERVAL 0 SECOND AS wfendtimestamp
         UNION ALL SELECT '2019-02-20 02:03:04' + INTERVAL 0 SECOND, '2019-02-21 02:58:38' + INTERVAL 0 SECOND
       ) t

返回:

wfstarttimestamp     wfendtimestamp         secs  
-------------------  -------------------  ------
2019-02-22 10:20:30  2019-02-22 10:20:45      15
2019-02-20 02:03:04  2019-02-21 02:58:38   89734