2015-11-06 84 views
0

我有一個非常討厭的查詢。我嘗試獲得按達陣排序的分類表...但在達到該目標之前,我有一個問題與一個總和...讓我解釋一下。總計字段與GROUP BY失敗

與此查詢:

SELECT tft.id_team 
    , z.id_partido 
    , y.td 
    FROM `team_faseTorneo` tft 
    LEFT 
    JOIN 
    (SELECT j.id_fase 
      , tp.id_team 
      , tp.id_partido 
     FROM jornadas j 
     LEFT 
     JOIN partidos USING(id_jornada) 
     JOIN team_partido tp USING(id_partido) 
    ) z 
    ON z.id_fase = tft.id_fase 
    AND z.id_team=tft.id_team 
    LEFT 
    JOIN 
    (SELECT p.id_team 
      , pL.id_partido 
      , pL.td 
     FROM players p 
     JOIN playerLogros pL USING(id_player) 
    ) y 
    ON y.id_team = tft.id_team 
    AND y.id_partido = z.id_partido 
WHERE tft.id_fase = 4 

結果是這樣的一個:

id_team id_partido td 
6 69 0 
6 69 0 
6 69 0 
6 69 0 
6 81 0 
6 81 0 
6 81 0 
6 81 1 
6 84 1 
6 84 0 
6 98 NULL 
6 99 NULL 
6 114 NULL 
6 116 NULL 
6 129 NULL 
6 133 NULL 
6 144 NULL 
6 150 NULL 
6 159 NULL 
6 167 NULL 
19 72 0 
19 72 0 
19 77 2 
19 77 0 
19 77 1 
19 77 0 
19 77 0 
19 89 0 
19 89 0 
19 92 NULL 

null是爲配襯(partidos)尚未發揮。我需要總結的比賽(黨)得分都達陣,所以我想這:

SELECT tft.id_team,z.id_partido, y.td FROM `team_faseTorneo` tft 
LEFT JOIN (SELECT j.id_fase, tp.id_team, tp.id_partido FROM jornadas j LEFT JOIN partidos USING(id_jornada) JOIN team_partido tp USING(id_partido) 
) z ON(z.id_fase=tft.id_fase AND z.id_team=tft.id_team) LEFT JOIN(SELECT p.id_team, pL.id_partido, SUM(pL.td) td FROM players p JOIN playerLogros pL USING(id_player) GROUP BY p.id_team) y ON(y.id_team=tft.id_team AND y.id_partido=z.id_partido) 
WHERE tft.id_fase=4 

但結果是這樣的:

id_team id_partido td 
6 69 NULL 
6 81 NULL 
6 84 NULL 
6 98 NULL 
6 99 NULL 
6 114 NULL 
6 116 NULL 
6 129 NULL 
6 133 NULL 
6 144 NULL 
6 150 NULL 
6 159 NULL 
6 167 NULL 
19 72 3 
19 77 NULL 
19 89 NULL 
19 92 NULL 
19 106 NULL 

我怎樣才能解決這個問題? 在此先感謝! :)

+0

應該怎樣的結果呢?雖然,如果是我,我會爲燈具和結果分開表格。 – Strawberry

回答

0

如果您需要總和,然後使用聚合查詢。也許是這樣的:

SELECT tft.id_team, z.id_partido, SUM(y.td) 
FROM `team_faseTorneo` tft LEFT JOIN 
     (SELECT j.id_fase, tp.id_team, tp.id_partido 
     FROM jornadas j LEFT JOIN 
      partidos 
      USING (id_jornada) JOIN 
      team_partido tp 
      USING (id_partido) 
    ) z 
     ON z.id_fase=tft.id_fase AND z.id_team=tft.id_team LEFT JOIN 
     (SELECT p.id_team, pL.id_partido, pL.td td 
     FROM players p JOIN 
      playerLogros pL 
      USING (id_player) 
    ) y 
     ON y.id_team=tft.id_team AND y.id_partido=z.id_partido 
WHERE tft.id_fase = 4 
GROUP BY tft.id_team, z.id_partido; 

您的查詢似乎過於複雜,但至少應該讓你的td總和。

0

我認爲我得到了它......

SELECT tft.id_team,z.id_partido, sum(z.td) FROM `team_faseTorneo` tft 
LEFT JOIN (SELECT j.id_fase, tp.id_team, tp.id_partido, y.td FROM jornadas j LEFT JOIN partidos p USING(id_jornada) JOIN team_partido tp USING(id_partido) LEFT JOIN(SELECT p.id_team, pL.id_partido, (pL.td) td FROM players p JOIN playerLogros pL USING(id_player)) y ON(y.id_team=tp.id_team AND y.id_partido=tp.id_partido) WHERE p.status='Finalizado' 
) z ON(z.id_fase=tft.id_fase AND z.id_team=tft.id_team) 
WHERE tft.id_fase=4 GROUP BY tft.id_team, z.id_partido ORDER BY tft.id_team