温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - How to convert average time returened from sql query to minutes
php sql time duration milliseconds

php - 如何将从SQL查询获得的平均时间转换为分钟

发布于 2020-03-29 13:14:57

我正在运行以下查询

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time))) as avg_time
FROM    Case_report
where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
  and Police_Circle="Sadar"

并从sql中获取正确的结果为“ 00:19:35.6000”

我需要在总分钟数中得到结果,例如20

查看更多

查看更多

提问者
Laraib Hassan
被浏览
52
Martin 2020-01-31 17:54

在SQL的这一部分中,您将总秒数转换为时间,这就是为什么将输出作为时间的原因:

SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time)))

无需将结果转换回时间,只需将秒除以60并四舍五入即可得到总分钟数:

ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60)

在您原始的SQL语句中:

SELECT  ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60) AS avg_time
  FROM  Case_report
  where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
        and Police_Circle="Sadar"