【问题标题】:What is the result of DateTime operation in MySQLMySQL中DateTime操作的结果是什么
【发布时间】:2019-12-01 08:18:36
【问题描述】:

我尝试了以下 SQL 查询。

select start_time, end_time, end_time-start_time from orders

这打印了以下输出。

start_time          |end_time             |end_time-start_time
...
2019-11-29 15:55:54 | 2019-12-01 15:59:00 | 72000346
...

我不知道结果是什么意思。 我知道我们可以使用 TIMESTAMPDIFF 来计算两个日期时间之间的差异。 有人知道结果是什么意思吗?

【问题讨论】:

  • 我想这个查询是通过使用 FRAC_SECOND 作为 5.5 之前版本的 MySQL DB 上的 TIMESTAMPDIFF 函数的单位来运行的。在 DB 版本中,TIMESTAMPDIFF 不存在这样的单元。 5.5+
  • 我认为这是一个错误,当这些时间戳被减去,并给出一个不清楚的值,可能是这个 bug 的剩余部分?
  • 简答:没什么用处

标签: mysql sql


【解决方案1】:
select 
  start_time,
  end_time, 
  '0000000000000000000'+end_time   as exp1,
  '0000000000000000000'-start_time as exp2,
  '0000000000000000000'+end_time - '0000000000000000000'-start_time      as exp3
from orders;

输出:

+---------------------+---------------------+----------------+-----------------+----------+
| start_time          | end_time            | exp1           | exp2            | exp3     |
+---------------------+---------------------+----------------+-----------------+----------+
| 2019-11-29 15:55:54 | 2019-12-01 15:59:00 | 20191201155900 | -20191129155554 | 72000346 |
+---------------------+---------------------+----------------+-----------------+----------+

What is the internal representation of timestamp values in MySQL

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-literals.html

【讨论】:

    【解决方案2】:

    MySQL 倾向于将日期/时间值视为数字或字符串,而不是日期/时间。通常,这并不重要,因为这些值很容易转换为日期/时间。

    但是,对于您问题中的值,表示为:

     2019-12-01 15:59:00 --> 20191201155900
     2019-11-29 15:55:54 --> 20191129155554 
    

    你得到的结果是20191201155900 - 20191129155554。

    发生这种情况是因为- 是数字运算符,并且数字具有更高的偏好。 + 也可能发生这种情况,但可以通过在表达式中添加 interval 来解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-07
      • 2019-10-07
      • 2016-10-12
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多