我有一个查询,我正在测试时间。
我有两次不同的时间,这让我怀疑数据的正确性。
以下是两个例子:
SELECT
users.fname as counselor,
count(session.anum) as students,
SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(starttime, signintime)))) AS 'AVG Wait Time',
SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(finishtime, starttime)))) AS 'AVG Session Length'
FROM session
LEFT JOIN support
ON support.session_id = session.session_id
LEFT JOIN users
ON users.username = support.counselor
WHERE session.status = 3
GROUP BY fname;
返回:
00:01:17 and 00:00:05
下一个查询是:
SELECT
users.fname as counselor,
count(session.anum) as students,
SEC_TO_TIME(AVG(TIMEDIFF(starttime, signintime))) AS 'AVG Wait Time',
SEC_TO_TIME(AVG(TIMEDIFF(finishtime, starttime))) AS 'AVG Session Length'
FROM session
LEFT JOIN support
ON support.session_id = session.session_id
LEFT JOIN users
ON users.username = support.counselor
WHERE session.status = 3
GROUP BY fname;
00:01:37 and 00:00:05
为什么会发生这种情况以及我应该相信哪种结果?如果有人能够清除我的困惑,那就爱。
答案 0 :(得分:3)
TIMEDIFF()返回一个字符串,例如
TIMEDIFF('13:07:33', '9:5:37') -> 04:01:56
如果您直接对其进行平均,则mysql会尝试将04:01:56
转换为int并最终使用avg(04)
。
这就是为什么你必须time_to_sec
,它将该时间值作为简单的int返回:14516,可以直接平均。