格式不正确
问题描述:
我有一个查询,让我查看正在运行的总在我的数据库在一定时间列,通过使用用户定义的变量:格式不正确
SET @total_duration := '00:00:00';
SELECT duration, (@total_duration := @total_duration + duration) AS cumulative_duration
FROM tbl_flights
然而,结果显示如下:
我敢肯定的运行总计是正确的,但它只是需要格式化,以看起来像hh:mm:ss
。我不知道如何做到这一点,如果任何人都可以帮助我,将不胜感激。
答
尝试使用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.)
现在的作品完美,谢谢。当它让我时,它会标记为正确。 – sinesine
您需要通过'order by'来执行此查询以产生一致的结果。 –
@vkp - 好点。谢谢。:-) – GurV