MySQL count(col)返回null

时间:2015-06-24 15:22:03

标签: mysql count

我有3张桌子:

  • 团队(id_team,name,id_season)
  • Teams_Stats(id_stats,id_game,id_team,胜利,失败)
  • 季节(id_season,name,nbr_teams)

我试图为每个球队取得特定赛季的胜利数量(本例中为2015年)。

这就是我目前所拥有的,但它为所有球队返回0。

我的SQL语法:

SELECT T.name, count(a.victory) as Wins
FROM Teams T, Seasons S
  LEFT JOIN 
    (SELECT TS.id_team, TS.victory FROM Teams_Stats TS WHERE TS.victory = 1) a
  ON a.id_team = 'T.id_team'
WHERE T.id_season = S.id_season AND S.name = '2015'
GROUP BY T.nom 
ORDER BY T.nom

我做错了什么?

谢谢:)

1 个答案:

答案 0 :(得分:0)

尝试修改您的查询有点像

SELECT T.name, count(TS.victory) as Wins
FROM Teams T
JOIN Seasons S ON T.id_season = S.id_season AND S.name = '2015'
LEFT JOIN Teams_Stats TS ON TS.id_team = T.id_team AND TS.victory = 1
GROUP BY T.name 
ORDER BY T.name