所以我每行有 2 个表,我用它来获得以分钟为单位的平均时间。
SELECT Sec_to_time (Avg (Timestampdiff (second, `survival`, `lastupdate`)))
FROM main
但我想单独获得小时、分钟和秒,这样我就可以在 php 中回显它们。
您可以使用 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
MySql中有hour()、minute()和second()函数。
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
尝试:
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;