timediff的意外结果

编程入门 行业动态 更新时间:2024-10-26 21:36:37
本文介绍了timediff的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

timediff函数不能按预期方式工作.在下面的示例中,我尝试计算1年内的秒数差异.

The timediff function does not work as expected. In the following example I am trying to calculate the difference in seconds for a period of 1 year.

mysql>SELECT 366*24*60*60 AS expected; +----------+ | expected | +----------+ | 31622400 | +----------+ 1 row in set (0.00 sec) mysql>SELECT ABS(UNIX_TIMESTAMP('2000:01:01 00:00:00') - UNIX_TIMESTAMP('2001:01:01 00:00:00')); +------------------------------------------------------------------------------------+ | ABS(UNIX_TIMESTAMP('2000:01:01 00:00:00') - UNIX_TIMESTAMP('2001:01:01 00:00:00')) | +------------------------------------------------------------------------------------+ | 31622400 | +------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>SELECT TIME_TO_SEC(TIMEDIFF('2000:01:01 00:00:00', '2001:01:01 00:00:00')); +---------------------------------------------------------------------+ | TIME_TO_SEC(TIMEDIFF('2000:01:01 00:00:00', '2001:01:01 00:00:00')) | +---------------------------------------------------------------------+ | -3020399 | +---------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec) Warning (Code 1292): Truncated incorrect time value: '-8784:00:00'

推荐答案

编辑:您正在使用哪个版本的MySQL?至少在5.0.22上可以正常工作.我只是跑了这个查询.看到这里

What version of MySQL are you using? It works fine on 5.0.22 at least. I just ran this query. see here

mysql> SELECT TIME_TO_SEC(TIMEDIFF('2000:01:01 00:00:00', '2001:01:01 00:00:00')); +---------------------------------------------------------------------+ | TIME_TO_SEC(TIMEDIFF('2000:01:01 00:00:00', '2001:01:01 00:00:00')) | +---------------------------------------------------------------------+ | -31622400 | +---------------------------------------------------------------------+ 1 row in set (0.00 sec)

您看到此处了吗? 可能是截断问题,因为时间范围比您的日期差小得多.

Did you see here? It may be truncation issue because Time range is much smaller than the date difference that you have.

原始答案

使用此

SELECT TIMESTAMPDIFF(SECOND,'2000:01:01 00:00:00', '2001:01:01 00:00:00'); +--------------------------------------------------------------------+ | TIMESTAMPDIFF(SECOND,'2000:01:01 00:00:00', '2001:01:01 00:00:00') | +--------------------------------------------------------------------+ | 31622400 | +--------------------------------------------------------------------+

问题是您正在尝试将负时间转换为秒.交换变量即可完成工作.

The problem is you're trying to convert a negative time to time-in-second. Swapping the variables will do the job.

SELECT TIME_TO_SEC(TIMEDIFF('2001:01:01 00:00:00','2000:01:01 00:00:00')); +--------------------------------------------------------------------+ | TIME_TO_SEC(TIMEDIFF('2001:01:01 00:00:00','2000:01:01 00:00:00')) | +--------------------------------------------------------------------+ | 31622400 | +--------------------------------------------------------------------+

更多推荐

timediff的意外结果

本文发布于:2023-11-13 14:13:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   timediff

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!