use*_*026 5 mysql date typeconverter
我使用decimal数据类型在MySQL中创建了一个名为'hours_spent'的字段来存储时间.这些值存储如1.30,2.30等......(1小时30分钟,2小时30分钟).
我想计算各种时间值的总和.
时间的总和不是我的预期:1.30 + 2.30 = 3.60,而我预期为4.00.
我使用MySQL中的SUM函数来计算hours_spent字段.如果值为0.30 + 1.50 = 1.80,而我预期为2.20.
我的第一个错误是使用十进制类型而不是时间数据类型,但我无法更改数据类型.
那么,有没有什么方法可以将时间值相加并得到我预期的结果?
谢谢
我在 sqlfiddle 为您准备了一个演示,如果您愿意,可以在那里尝试一下:
http://www.sqlfiddle.com/#!2/c9afc/2
以下是查询示例:
select @indexer:=instr(dateasdecimal, '.')
, left(dateasdecimal, @indexer-1) * 60 + substr(dateasdecimal, @indexer+1) as totalMinutes
from testtable;
select @indexer:=instr(dateasdecimal, '.')
, sum(left(dateasdecimal, @indexer-1) * 60 + substr(dateasdecimal, @indexer+1)) as totalMinutes
from testtable;
Run Code Online (Sandbox Code Playgroud)
注意:请不要忘记接受您问题的答案: https://meta.stackexchange.com/a/65088/200585