我试图计算从mysql查询中获得的值的差异,其中数据值是在TIME格式的竞争时间段中提取的。目标是获得每圈到达之间的单圈时间。这是表Times
。
id number time
---------+----------------+--------------------
1 9 00:00:02.000000
2 10 00:00:14.000000
3 9 00:11:09.000000
Mysql
SELECT
t.number, count(*) as laps,
group_concat(time order by t.id separator ',' ) as times,
SEC_TO_TIME(SUM(TIME_TO_SEC(`time`))) as total
FROM
times t
GROUP BY
number
ORDER BY
laps DESC, total ASC
此查询的输出为:
number laps times total
--------------------------------------------------------------
9 2 00:00:02,00:00:14 00:00:16.000000
10 1 00:11:09 00:11:09.000000
现在,我需要的是获得每圈的时间长度,以及减法。 有任何建议请。感谢这个结果:
number laps times total
--------------------------------------------------------------
9 2 00:00:02,00:00:12 00:00:14.000000
10 1 00:11:09 00:11:09.000000
答案 0 :(得分:1)
似乎你有总时间存储并且想要计算单圈时间。一种方法是使用子查询;
SELECT t.number, COUNT(1) laps,
GROUP_CONCAT(SEC_TO_TIME(time) ORDER BY t.id) times,
SEC_TO_TIME(SUM(time)) total
FROM (
SELECT t1.id, t1.number,
TIME_TO_SEC(t1.time) - COALESCE(SUM(TIME_TO_SEC(t2.time)), 0) time
FROM times t1
LEFT JOIN times t2 ON t1.number = t2.number AND t2.id < t1.id
GROUP BY t1.id, t1.number, t1.time
) t
GROUP BY number
子查询通过从每圈结束的总时间中减去id较小的相同数字的所有时间来计算单圈时间。外部查询以您正在寻找的格式对单圈时间进行格式化。