Time()函数return Null [duplicate]

问题描述 投票:-1回答:1
我有一个具有

'duration'列的表,该表以时间格式存储持续时间,即数据类型为'time'。当我根据条件按特定组计算此“持续时间”列的平均值时,它给出以下输出信息:

以下为查询:

select avg(Cactive_duration) as 'duration' from table_name group by column_name;

enter image description here

但是当我尝试将它们转换为时间格式时,输出如下:

以下为查询:

select time(avg(Cactive_duration)) as 'duration' from table_name group by column_name;

enter image description here

非常感谢您的帮助。该数据库是MySQL。

mysql sql
1个回答
0
投票
简短的答案是:MySQL无法汇总时间。

最好的是,MySQL通过引发异常来承认这一点。相反,它将时间转换为某个数字(似乎是内部表示形式)并进行处理,从而导致没人想要的数字。

改为将您的时间转换为秒,进行汇总,然后转换回时间:

select sec_to_time(avg(time_to_sec(cactive_duration))) as duration from table_name group by column_name;

© www.soinside.com 2019 - 2024. All rights reserved.