使用MySQL查询选择多个总和,并将它们显示在单独的列中

问题描述:

假设我有一个像这样的假设表,它记录了某些游戏中某个玩家得分的时间:

Let's say I have a hypothetical table like so that records when some player in some game scores a point:

name   points
------------
bob     10
mike    03
mike    04
bob     06

如何获取每个球员的得分总和并在一个查询中并排显示?

How would I get the sum of each player's scores and display them side by side in one query?

总积分表

bob   mike
16     07

我的(伪)查询是:

SELECT sum(points) as "Bob" WHERE name="bob",
       sum(points) as "Mike" WHERE name="mike"
  FROM score_table

您可以手动"操作数据:

You can pivot your data 'manually':

SELECT SUM(CASE WHEN name='bob' THEN points END) as bob,
       SUM(CASE WHEN name='mike' THEN points END) as mike
  FROM score_table

,但是如果您的播放器列表是动态的,则此操作将无效.

but this will not work if the list of your players is dynamic.