首页 > 解决方案 > 如何将从sql查询返回的平均时间转换为分钟

问题描述

我正在运行以下查询

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

标签: phpsqltimedurationmilliseconds

解决方案


在这部分 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"

推荐阅读