本文介绍了使用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.

这篇关于使用MySQL查询选择多个总和,并将它们显示在单独的列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:01