我有这个问题:
SELECT sum(TIMEDIFF(`time_out`, `time_in`)) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'
然后我计算秒到小时:
$total_difference = (($difference->total) / 60 / 60);
但这显示我22,352125小时,这是不可能的......
我一直在测试,我有2条记录显示如果我不使用SUM :
SELECT TIMEDIFF(`time_out`, `time_in`) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'
这个结果远远低于我在查询中使用SUM时得到的22小时。有帮助吗?我不知道我做错了什么。
谢谢!
答案 0 :(得分:3)
可以调整查询以使用TIME_TO_SEC - 这会将TIMEDIFF的返回值转换为数字秒数,这可以合理地求和
调整后的查询
SELECT SUM(TIME_TO_SEC(TIMEDIFF(time_out, time_in))) as total
FROM user_log
WHERE uid = '1' AND time_out <> '0000-00-00 00:00:00'
;