我有mysql列时间(3)并且它存储了很好的时间值..但是后来我要总结两次它转换为坏时间格式;
我有两个记录:
id | time
---|-----------
1 | 00:00:15.490
2 | 00:02:14.900
Run Code Online (Sandbox Code Playgroud)
所以在真实中我应该得到: 00:02:30.390
但我明白了 230.390
无论如何从Mysql得到正确的答案?PS我使用PHP的功能,但不想使用它,除非有其他方法.需要与MILLISECONDS相加
现在我正在使用查询 SELECT SUM(time) AS total_time FROM times WHERE 1
如果您的表定义是这样的:
create table test (
id integer,
`time` time(3) -- important to specify precision
);
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
select time(sum(`time`))
from test;
Run Code Online (Sandbox Code Playgroud)
注意:需要 mysql 5.6+
编辑
实际上,这time是使用错误的功能,因为它没有很多智能。
使用sec_to_time代替,即:
select sec_to_time(sum(`time`))
from test;
Run Code Online (Sandbox Code Playgroud)
编辑time提取时间值,sec_to_time计算时间值——即,time(70)返回 NULL,因为没有 70 秒的有效时间,对于相同的输入
sec_to_time'00:01:10',as将正确返回
原来我还是错了。让我们尝试将毫秒与其余时间分开处理:
select sec_to_time(sum(time_to_sec(`time`)) + sum(microsecond(`time`))/1000000)
from test;
Run Code Online (Sandbox Code Playgroud)