Left Join使不同表中的两个计数具有相同的值

时间:2013-05-03 17:41:36

标签: mysql sql

我有很多LEFT JOINS的查询。问题是我有两个似乎不能一起工作的COUNTS。即使它们不是,这两个计数也将等于相同的值。

这就是我所拥有的;

SELECT T.ID,
       T.name,
       T.pic,
       T.T_ID,
       COUNT(P.T_ID) AS plays,
       COUNT(L.T_ID) AS likes,
       S.Status,
       G.gig_name,
       G.date_time,
       G.lineup,
       G.price,
       G.ticket,
       E.action,
       E.ID,
       E.timestamp,
       E.E_ID
  FROM events E
       LEFT JOIN TRACKS T
       ON T.ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = T.T_ID
       LEFT JOIN STATUS S
       ON S.ID = E.ID AND E.action = 'has some news.' AND E.E_ID = S.S_ID
       LEFT JOIN GIGS G
       ON G.ID = E.ID AND E.action = 'has posted a gig.' AND E.E_ID = G.G_ID
       LEFT JOIN track_plays P
       ON P.A_ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = P.T_ID
       LEFT JOIN track_likes L
       ON L.ID = E.E_ID AND E.action = 'has uploaded a track.'
 WHERE E.ID = '3'
 GROUP BY E.E_ID
 ORDER BY E.timestamp DESC LIMIT 15

我不会解释所有查询,但我认为你会得到它的要点。有问题的JOINS是最后两个。如果COUNT(P.T_ID) = 100 COUNT(L.T_ID)也将= 100

提前致谢!

4 个答案:

答案 0 :(得分:1)

您应该尝试使用COUNT(DISTINCT)

SELECT T.ID,
       T.name,
       T.pic,
       T.T_ID,
       COUNT(DISTINCT P.T_ID) AS plays,
       COUNT(DISTINCT L.T_ID) AS likes,
       S.Status,
       G.gig_name,
       G.date_time,
       G.lineup,
       G.price,
       G.ticket,
       E.action,
       E.ID,
       E.timestamp,
       E.E_ID
  FROM events E
       LEFT JOIN TRACKS T
       ON T.ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = T.T_ID
       LEFT JOIN STATUS S
       ON S.ID = E.ID AND E.action = 'has some news.' AND E.E_ID = S.S_ID
       LEFT JOIN GIGS G
       ON G.ID = E.ID AND E.action = 'has posted a gig.' AND E.E_ID = G.G_ID
       LEFT JOIN track_plays P
       ON P.A_ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = P.T_ID
       LEFT JOIN track_likes L
       ON L.ID = E.E_ID AND E.action = 'has uploaded a track.'
 WHERE E.ID = '3'
 GROUP BY E.E_ID
 ORDER BY E.timestamp DESC LIMIT 15

答案 1 :(得分:1)

您可以尝试将这些计数“重新表述”为相关子查询:

SELECT T.ID,
       T.name,
       T.pic,
       T.T_ID,
       (SELECT COUNT(*) FROM track_plays WHERE A_ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = T_ID) AS plays,
       (SELECT COUNT(*) FROM track_likes WHERE ID = E.E_ID AND E.action = 'has uploaded a track.') AS likes,
       S.Status,
       G.gig_name,
       G.date_time,
       G.lineup,
       G.price,
       G.ticket,
       E.action,
       E.ID,
       E.timestamp,
       E.E_ID
  FROM events E
       LEFT JOIN TRACKS T
       ON T.ID = E.ID AND E.action = 'has uploaded a track.' AND E.E_ID = T.T_ID
       LEFT JOIN STATUS S
       ON S.ID = E.ID AND E.action = 'has some news.' AND E.E_ID = S.S_ID
       LEFT JOIN GIGS G
       ON G.ID = E.ID AND E.action = 'has posted a gig.' AND E.E_ID = G.G_ID
 WHERE E.ID = '3'
 GROUP BY E.E_ID
 ORDER BY E.timestamp DESC LIMIT 15

我发现这更容易理解/维护并且通常更快。

答案 2 :(得分:0)

两个COUNT都没有WHERE子句并出现在同一个表中,即结果表。因此,两者都报告结果表的总行数。

答案 3 :(得分:0)

可能是我错了,但你只计算了#vresult行的数量,不是吗?

可能需要将计数更改为

select count(P_ID) from track_plays where p_id = E.ID