我正在测试查询,以确保它们返回正确的结果。然而,我注意到我弄错了时间。我的时间戳之一是
2013-02-07 07:26:04
我减去
2013-02-07 07:18:23
从第一个时间戳
这给了我十二分钟左右的时间差。现在我不是一个数学天才,但我甚至可以说18-12=6…不是12…所以我知道这个查询有问题。所以我删除了SEC_TO_TIME,它给了我正确的时间差:
+------------+-----------+--------------+----------------+--------+----------+
| 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
当我在TIMEDIFF之前添加SEC_TO_TIME时,问题就出现了。
为什么会发生这种情况?
现在,向大家展示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)
任何解释都很好。
00:07:41=741 seconds=00:12:21是SEC_TO_TIME对它的看法。该函数需要一些秒,并将其格式转换为h:m:i。您已经将它作为h:m:i,在这里使用SEC_to_TIME是没有意义的。
您已经有了时间格式,所以您可能希望将时间转换为秒
TIME_TO_SEC()
diff函数返回一个时间,而不是秒,那么你应该使用TIME_TO_SEC()