我有一个查询,允许我通过使用用户定义的变量来查看数据库中某个特定时间列的运行总计:
SET @total_duration := '00:00:00';
SELECT duration, (@total_duration := @total_duration + duration) AS cumulative_duration
FROM tbl_flights
然而,结果如下所示:
我非常确定正在运行的总数是正确的,但它只需要格式化为hh:mm:ss
。我不知道怎么做,如果有人能帮助我,我将不胜感激。
答案 0 :(得分:3)
尝试使用time_to_sec
和sec_to_time
函数:
order by
需要获得一致的结果。假设您想要按一列或多列的递增顺序找到此累积总和,请说flight_id
:
SET @total_duration := 0;
SELECT
duration,
sec_to_time(@total_duration := @total_duration
+ time_to_sec(duration)) AS cumulative_duration
FROM tbl_flights
ORDER BY flight_id -- required to get consistent results
-- (change the column name in "order by" as needed.)