如何在MySQL中对游戏列进行总计和显示总计

时间:2014-05-13 21:00:23

标签: mysql

以下是以下查询的结果

Select
 MOM_rebels_cms.log.AGE,
 MOM_rebels_cms.log.TEAM,
 MOM_rebels_cms.log.LAST_NAME,
 MOM_rebels_cms.log.NUMBER,
 MOM_rebels_cms.log.Game_1,
 MOM_rebels_cms.log.Game_2,
 MOM_rebels_cms.log.Game_3,
 MOM_rebels_cms.log.Game_4,
 MOM_rebels_cms.log.Game_5
From
 MOM_rebels_cms.log

id | AGE | TEAM | LAST_NAME | NUMBER | GAME_1 | GAME_2 | GAME_3 | GAME_4
 1 |  9  |Rebels|   SMITH   |   22   |    3   |    1   |    2   |    2

我想在同一行显示Game_x列的总计,如:

id | AGE | TEAM | LAST_NAME | NUMBER | GAME_1 | GAME_2 | GAME_3 | GAME_4 | TOTAL
 1 |  9  |Rebels|   SMITH   |   22   |    3   |    1   |    2   |    2   |   8

我可以使用总数:

 Select
  MOM_rebels_cms.log.AGE,
  MOM_rebels_cms.log.TEAM,
  MOM_rebels_cms.log.LAST_NAME,
  MOM_rebels_cms.log.NUMBER,
  MOM_rebels_cms.log.Game_1 +
  MOM_rebels_cms.log.Game_2 +
  MOM_rebels_cms.log.Game_3 +
  MOM_rebels_cms.log.Game_4
  AS TOURNEYSum
 From
  MOM_rebels_cms.log

但它只显示:

id | AGE | TEAM | LAST_NAME | NUMBER | TOTAL
 1 |  9  |Rebels|   SMITH   |   22   |   8

我想显示个人游戏结果以及总数。

3 个答案:

答案 0 :(得分:1)

你总是使用游戏,但没有选择它们。

 Select
  MOM_rebels_cms.log.AGE,
  MOM_rebels_cms.log.TEAM,
  MOM_rebels_cms.log.LAST_NAME,
  MOM_rebels_cms.log.NUMBER,
  MOM_rebels_cms.log.Game_1,
  MOM_rebels_cms.log.Game_2,
  MOM_rebels_cms.log.Game_3,
  MOM_rebels_cms.log.Game_4,
  MOM_rebels_cms.log.Game_1 +
  MOM_rebels_cms.log.Game_2 +
  MOM_rebels_cms.log.Game_3 +
  MOM_rebels_cms.log.Game_4
  AS TOURNEYSum
 From
  MOM_rebels_cms.log

将单个游戏添加到您的查询中就可以了。

答案 1 :(得分:0)

假设每行中的游戏数量相同,您只需要单独选择列以及总数:

Select
  MOM_rebels_cms.log.AGE,
  MOM_rebels_cms.log.TEAM,
  MOM_rebels_cms.log.LAST_NAME,
  MOM_rebels_cms.log.NUMBER,
  MOM_rebels_cms.log.Game_1,
  MOM_rebels_cms.log.Game_2,
  MOM_rebels_cms.log.Game_3,
  MOM_rebels_cms.log.Game_4,
  MOM_rebels_cms.log.Game_1 +
  MOM_rebels_cms.log.Game_2 +
  MOM_rebels_cms.log.Game_3 +
  MOM_rebels_cms.log.Game_4
  AS TOURNEYSum
 From
  MOM_rebels_cms.log

答案 2 :(得分:0)

您可以一次选择单个值和总和。稍微简化一下;

SELECT id, AGE, TEAM, LAST_NAME, NUMBER,
       Game_1,  Game_2,  Game_3,  Game_4,
       Game_1 + Game_2 + Game_3 + Game_4 AS TOTAL
FROM MOM_rebels_cms.log