2017-02-13 128 views
1

我有一个查询,让我查看正在运行的总在我的数据库在一定时间列,通过使用用户定义的变量:格式不正确

SET @total_duration := '00:00:00'; 
SELECT duration, (@total_duration := @total_duration + duration) AS cumulative_duration 
FROM tbl_flights 

然而,结果显示如下:

enter image description here

我敢肯定的运行总计是正确的,但它只是需要格式化,以看起来像hh:mm:ss。我不知道如何做到这一点,如果任何人都可以帮助我,将不胜感激。

回答

3

尝试使用time_to_secsec_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.) 
+0

现在的作品完美,谢谢。当它让我时,它会标记为正确。 – sinesine

+0

您需要通过'order by'来执行此查询以产生一致的结果。 –

+0

@vkp - 好点。谢谢。:-) – GurV