MySQL最早的DATETIME JOIN

时间:2014-12-04 14:24:25

标签: mysql sql

此查询按照分配给谁的方式对故障单进行分组,并计算出故障单关闭的平均舍入天数。

SELECT a.id as theuser, round(avg(DATEDIFF( ta.dateClosed, t.dateAded ) * 1.0), 2) as avg
FROM tickets t join
     mdl_user a
     on find_in_set(a.id, t.assignedto) > 0
GROUP BY a.id ORDER BY avg ASC

我现在想加入ticketanswer表,找出第一次回复的平均时间。 票可能有多个答案所以我只想得到第一个。 因此,我试图更改查询以包含此无效。任何人都可以了解我做错了什么?

SELECT a.id as theuser, round(avg(DATEDIFF( ta.dateAded , t.dateAded ) * 1.0), 2) as avg
FROM tickets t join
     mdl_user a
     on find_in_set(a.id, t.assignedto) > 0
INNER JOIN (SELECT MIN(ta.dateAded) as started FROM ticketanswer GROUP BY ta.ticketId) ta ON t.id = ta.ticketId
GROUP BY a.id ORDER BY avg ASC

1 个答案:

答案 0 :(得分:1)

对您的查询稍作修改。

SELECT a.id as theuser, round(avg(DATEDIFF( ta.dateAded , t.dateAded ) * 1.0), 2) as avg
FROM tickets t join
 mdl_user a
 on find_in_set(a.id, t.assignedto) > 0
INNER JOIN (SELECT ticketid, MIN(dateAded) as started FROM ticketanswer GROUP BY ticketId) ta ON t.id = ta.ticketId
GROUP BY a.id ORDER BY avg ASC