简体   繁体   English

SUM不显示正确的MySQL和

[英]SUM not display correct sum MySQL

I have this query: 我有这个问题:

SELECT sum(TIMEDIFF(`time_out`, `time_in`)) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'

Then I calculate the seconds to hours: 然后我计算秒到小时:

$total_difference = (($difference->total) / 60 / 60);

But this is showing me 22,352125 hours, which is impossible... 但这显示我22,352125小时,这是不可能的......

I've been testing and I have 2 records showing up if I don't use SUM : 我一直在测试, 如果我不使用SUM ,我会显示2条记录:

SELECT TIMEDIFF(`time_out`, `time_in`) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'
  1. 08:00:02 8时00分02秒
  2. 00:00:11 00:00:11

This result is far below the 22 hours I get when I use SUM in my query. 这个结果远远低于我在查询中使用SUM时得到的22小时。 Any help? 有帮助吗? I don't know what I'm doing wrong. 我不知道我做错了什么。

Thanks! 谢谢!

Can adjust the query to use TIME_TO_SEC - this will convert the return value of TIMEDIFF to a numeric number of seconds, which can sensibly be summed 可以调整查询以使用TIME_TO_SEC - 这会将TIMEDIFF的返回值转换为数字秒数,这可以合理地求和

adjusted query 调整后的查询

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'
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM