Jeroen Bellemans
Jeroen Bellemans

Reputation: 2035

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...

I've been testing and I have 2 records showing up if I don't use SUM:

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
  2. 00:00:11

This result is far below the 22 hours I get when I use SUM in my query. Any help? I don't know what I'm doing wrong.

Thanks!

Upvotes: 1

Views: 81

Answers (1)

amdixon
amdixon

Reputation: 3833

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

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

Upvotes: 3

Related Questions