RaGe10940
RaGe10940

Reputation: 667

SEC_TO_TIME time error

I was testing out queries to make sure that they were giving back proper results. However I noticed that I was getting the wrong time. One of my timestamps is

2013-02-07 07:26:04

and I subtract :

2013-02-07 07:18:23

from the first time stamp

and that was giving me a difference of twelve or so minutes. Now I'm not a math genius but I can even say that 18 - 12 = 6... not 12... so I knew there was a problem with the query. So I removed the SEC_TO_TIME and it gave me the correct time diff of :

+------------+-----------+--------------+----------------+--------+----------+
| session_id | anum      | first        | last           | why    | time     |
+------------+-----------+--------------+----------------+--------+----------+
|        220 | B00000000 | Testing      | thisout        | Other  | 00:07:41 |
+------------+-----------+--------------+----------------+--------+----------+


This is my query now : 


SELECT 
session.session_id, 
session.anum, 
student.first, 
student.last, 
session.why,
(TIMEDIFF(t.fin, session.signintime)) AS time 
FROM session 
INNER JOIN student 
    ON session.anum = student.anum  
LEFT JOIN (SELECT support.session_id, MAX(support.finishtime) AS fin FROM support GROUP BY support.session_id) AS t  
    ON t.session_id = session.session_id 
WHERE session.status = 3 

The problem comes when I add the SEC_TO_TIME before the TIMEDIFF.

Why does this happen?

Now just to show you guys the result of the query with SEC_TO_TIME :

+------------+-----------+--------------+----------------+--------+----------+
| session_id | anum      | first        | last           | why    | time     |
+------------+-----------+--------------+----------------+--------+----------+
|        220 | B00000000 | Testing      | thisout        | Other  | 00:12:21 |
+------------+-----------+--------------+----------------+--------+----------+
2 rows in set (0.00 sec)

Any explanation would be lovely.

Upvotes: 1

Views: 687

Answers (2)

echo_Me
echo_Me

Reputation: 37253

you already have time format , so maybe you looking to convert time to seconds

TIME_TO_SEC()

your diff function returns a time , not seconds , then u should use TIME_TO_SEC()

Upvotes: 2

ickmund
ickmund

Reputation: 289

00:07:41 = 741 seconds = 00:12:21 is how SEC_TO_TIME sees it. The function takes a number of seconds and converts format it as h:m:i. You already have it as h:m:i, it makes no sense to use SEC_TO_TIME here.

Upvotes: 2

Related Questions