GROUP BY的sum字段失败

时间:2015-11-06 11:59:47

标签: mysql

我有一个非常讨厌的查询。我尝试按触地顺序排序分类表...但在达到目标之前,我遇到了一个问题...让我解释一下。

使用此查询:

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)。我需要总结比赛(partido)得分的所有达阵,所以我尝试了这个:

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

我该如何解决这个问题? 提前致谢!! :)

2 个答案:

答案 0 :(得分: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的总和。

答案 1 :(得分:0)

我认为我明白了......

# mm to inch
setWidth = 183*0.039370 

# font size in pt
setFontSize = 7

# 1 in R = 0.75pt, so 0.25pt is specified as 
setLwd <- 0.25/0.75    


pdf(file='filename.pdf',width=setWidth,height=3.5,pointsize=setFontSize)
plot(x,y,lwd=setLwd)
.....
dev.off()