php - MySQL SUM time(3) with milliseconds -


i have mysql column time(3) , it's storing value of time.. want sum 2 times converts bad time format;

i have 2 records:

id | time ---|----------- 1  | 00:00:15.490 2  | 00:02:14.900 

so in real shoud get: 00:02:30.390

but 230.390

is anyway correct answer mysql? p.s. using php functions dont want use it, unless there other way. need sum times milliseconds

for using query select sum(time) total_time times 1

provided table definition this:

create table test (   id integer,   `time` time(3)   -- important specify precision ); 

you can this:

select time(sum(`time`))   test; 

note: requires mysql 5.6+ edit

actually, time wrong function use, doesn't have many smarts.

use sec_to_time instead, ie:

select sec_to_time(sum(`time`))   test; 

time extracts time value, sec_to_time calculates time value -- ie, time(70) returns null because there's no valid time has 70 seconds, sec_to_time correctly return '00:01:10' same input edit

turns out i'm still wrong. lets try treating milliseconds separately rest of time:

select sec_to_time(sum(time_to_sec(`time`)) + sum(microsecond(`time`))/1000000)   test; 

Comments