全部, 我有一个名为duration的MySQL表,其中包含一天内访问过的不同位置的名称和总持续时间。我需要总计时间并为每个人显示它们(以小时,分钟,秒格式)。我相信我需要先转换为秒,然后求和并转换回hh:mm:ss)但我正在努力使用正确的SQL代码(不想使用存储过程)。任何帮助表示赞赏。
FROM jenkins/jenkins:lts
RUN apt-get update && apt-get install -y maven
即。 Jack的总持续时间04:28:37,Stacy的总持续时间22:56:34
答案 0 :(得分:3)
select name, SEC_TO_TIME( SUM( time_to_sec( duration ) ) )
from yourtable
group by name;
例如 - 获得总和:
MariaDB [(none)]> select SEC_TO_TIME(sum(time_to_sec( duration ))) from ( select '01:00:00' as duration union all select '00:35:03' as duration ) as test;
+-------------------------------------------+
| SEC_TO_TIME(sum(time_to_sec( duration ))) |
+-------------------------------------------+
| 01:35:03.000000 |
+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> select TIME_FORMAT(SEC_TO_TIME(sum(time_to_sec( duration ))),'%H:%i:%S') from ( select '01:00:00' as duration union all select '00:35:03' as duration ) as test;
+-------------------------------------------------------------------+
| TIME_FORMAT(SEC_TO_TIME(sum(time_to_sec( duration ))),'%H:%i:%S') |
+-------------------------------------------------------------------+
| 01:35:03 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
测试组总和
MariaDB [(none)]> select name,TIME_FORMAT(SEC_TO_TIME(sum(time_to_sec( duration ))),'%H:%i:%S') from ( select 'a' as name, '01:00:00' as duration union all select 'a' as name, '00:35:03' as duration union all select 'b' as name, '05:26:00' as duration ) as test group by name;
+------+-------------------------------------------------------------------+
| name | TIME_FORMAT(SEC_TO_TIME(sum(time_to_sec( duration ))),'%H:%i:%S') |
+------+-------------------------------------------------------------------+
| a | 01:35:03 |
| b | 05:26:00 |
+------+-------------------------------------------------------------------+
2 rows in set (0.00 sec)