时间戳差异,仅获得小时,分钟和秒

时间:2012-08-20 17:44:53

标签: mysql time diff

所以我每行有2个表,我用它来得到平均时间的平均时间。

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

但我想单独得到小时,分钟和秒钟,这样我就能在php中回应它们。

3 个答案:

答案 0 :(得分:3)

您可以使用MySQL HOUR(), MINUTE() and 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 Here

答案 1 :(得分:0)

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

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

答案 2 :(得分: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;