我有一列名为time_spent,它类似于:hh:mm:ss。例如00:23:11,这意味着23分11秒。我不知道如何将这些加起来。比如说00:23:11 + 00:10:20 = 00:33:31。我不确定如何直接在命令中执行此操作。我曾经尝试过convert和CAST,但最终失败了。任何帮助,将不胜感激。
- 积分
0 - 话题
0 - 评论
3136 - 注册排名
2289
我有一列名为time_spent,它类似于:hh:mm:ss。例如00:23:11,这意味着23分11秒。我不知道如何将这些加起来。比如说00:23:11 + 00:10:20 = 00:33:31。我不确定如何直接在命令中执行此操作。我曾经尝试过convert和CAST,但最终失败了。任何帮助,将不胜感激。
You can turn your times to seconds using
time_to_sec()
, sum them, and then turn the result to a time usingsec_to_time()
.As an example, the following query would compute the total
time_spent
over the whole table:This will work even it your times are stored as string, since their format complies to MySQL format for the
time
datatype.Note that MySQL
time
datatype can handle values between '-838:59:59' to '838:59:59' only. If your total time is greater than that, then you will not be able to convert it back to atime
.Once you store dates in
DATE
,TIME
andDATETIME
formats there are a multitude of available date and time functions you can use.