GROUP BY语句的问题(SQL)

时间:2011-02-22 16:54:57

标签: sql mysql group-by

我有一张GAMES表,其中包含以下信息:

Id_Game  Id_Player1  Id_Player2  Week
--------------------------------------
1211     Peter       John        2
1215     John        Louis       13
1216     Louis       Peter       17

我想获得每个玩家玩过的最后一周的列表,以及应该是这样的游戏数量:

Id_Player  Week  numberGames
-----------------------------
Peter      17    2
John       13    2
Louis      17    2

但我得到了这个(关于彼得周的通知)

Id_Player  Week  numberGames
-----------------------------
Peter      2     2
John       13    2
Louis      17    2

我的工作是:

  SELECT Id_Player, 
         MAX(Week) AS Week, 
         COUNT(*) as numberGames 
    FROM ((SELECT Id_Player1 as Id_Player, Week 
             FROM Games) 
           UNION ALL
           (SELECT Id_Player2 as Id_Player, Week 
              FROM Games)) AS g2 
GROUP BY Id_Player;

有人能帮助我找到错误吗?

1 个答案:

答案 0 :(得分:7)

Week列的数据类型是什么?如果Week的数据类型是varchar,则会出现此行为。