TIME_TO_SEC 在 mysql 中无法正常工作
“选择
SEC_TO_TIME(SUM(TIME_TO_SEC(m.totalTime))) 作为totalDuration 从米 内连接 pd on pd.candidateId = m.candidateId 其中 m.s_id 不为空 and pd.s = 1"
OUTPUT :totalDuration: 838:59:59
"select
(SUM(TIME_TO_SEC(m.totalTime))/3600) as totalDuration
from m
inner join pd on pd.candidateId = m.candidateId
where m.s_Id is not null
and pd.s = 1"
Output:totalDuration:1207.7658
我的问题是为什么 TIME_TO_SEC 函数不返回所需的输出 就像第一个查询持续时间是 838:59:59 一样,在秒查询中除以 3600 会显示不同的结果 1207.7658(hr)
" select
SEC_TO_TIME(SUM(TIME_TO_SEC(m.totalTime))) as totalDuration
from m
inner join pd on pd.candidateId = m.candidateId
where m.s_id is not null
and pd.s = 1"
OUTPUT :totalDuration: 838:59:59
"select
(SUM(TIME_TO_SEC(m.totalTime))/3600) as totalDuration
from m
inner join pd on pd.candidateId = m.candidateId
where m.s_Id is not null
and pd.s = 1"
Output:totalDuration:1207.7658
My question is why TIME_TO_SEC Function not returning desire output
like in first query duration is 838:59:59 and in sec query by dividing 3600 it is shown different result 1207.7658(hr)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果参数为 3020399 或更大,则
SEC_TO_TIME()
返回'838:59:59'
。在您的情况下,
SUM(TIME_TO_SEC(m.totalTime))
表达式的值似乎是4347957
。它更大,因此函数返回 TIME 数据类型的最大值。但是,当您除以不转换为 TIME 的值时,将使用原始值进行除法。
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=43166d9756e68c60c9abdbf43136b340
SEC_TO_TIME()
returns'838:59:59'
if an argument is 3020399 or greater.In your case the value of
SUM(TIME_TO_SEC(m.totalTime))
expression seems to be4347957
. It is greater, so the function returns maximal value for TIME datatype.But when you divide w/o convertion to TIME then the original value is used for division.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=43166d9756e68c60c9abdbf43136b340
SEC_TO_TIME() 的范围是 -838:59:59 到 838:59:59。因此,当我们将秒转换为时间时,如果时间大于 838:59:59,在我的情况下,它总是返回 838:59:59 值,这是同样的问题。
所以我将秒除以 3600 转换为小时。
Range of SEC_TO_TIME() is -838:59:59 to 838:59:59. So, when we are converting seconds to time if time is greater than 838:59:59 it will always return 838:59:59 value in my case that is same issue .
so I have converted sec to hour by divided by 3600.