0

所以我每行有 2 个表,我用它来获得以分钟为单位的平均时间。

SELECT Sec_to_time (Avg (Timestampdiff (second, `survival`, `lastupdate`)))
FROM   main

但我想单独获得小时、分钟和秒,这样我就可以在 php 中回显它们。

4

3 回答 3

3

您可以使用 MySQL HOUR()、MINUTE() 和 SECOND()函数将 SEC_TO_TIME 返回的时间分解为其组件。

为了避免重复自己,您可以包装计算:

SELECT HOUR(x.timeDiff), MINUTE(x.timeDiff), SECOND(x.timeDiff) 
FROM 
  (
    SELECT Sec_to_time(Avg (Timestampdiff (second, `survival`, `lastupdate`))) as timeDiff
    FROM main
  ) AS x

SqlFiddle 在这里

于 2012-08-20T17:50:03.883 回答
0

MySql中有hour()、minute()和second()函数。

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

于 2012-08-20T17:50:46.823 回答
0

尝试:

select concat(HOUR(Sec_to_time(Avg (Timestampdiff (second, login_datetime, logout_datetime)))),
  'hr ',
  MINUTE(Sec_to_time(Avg (Timestampdiff (second, login_datetime, logout_datetime)))),
  'min ',
  SECOND(Sec_to_time(Avg (Timestampdiff (second, login_datetime, logout_datetime)))), 
  'sec ') 
as his_time from table_name; 
于 2012-08-21T14:38:53.477 回答