简体   繁体   English

MySQL从多个总和中选择最大值

[英]mysql select greatest value from multiple sum

I am using the following results table 我正在使用以下结果表

Results Table
__________________________________________
player_a| player_b | player_c |   year 
________|________  |__________|___________|
100      150        150        2015  
100      -50        -50        2015 
100      350        250        2014
200      350        250        2014  

What i would like to do is get the sum for each player per year. 我想做的就是获取每个球员每年的总和。 Using the following Select i get the desired results. 使用以下选择我将获得所需的结果。

SELECT (

SELECT SUM( player_a )
FROM `results`
WHERE year =2015
) AS player_a_2015, (

SELECT SUM( player_a)
FROM `results`
WHERE year =2014
) AS player_a_2014

How can i get the maximum sum result only ? 我怎样才能只获得最大的总和结果?

ie For player_a should be 300 即对于player_a应该是300

SELECT SUM(player_a) AS spa
FROM results
GROUP BY year
ORDER BY spa DESC
LIMIT 1

This should do it. 这应该做。

Make sure you wrap your OR condition in parenthesis so it evaluates them both. 确保将“或”条件包装在括号中,以便对它们都进行评估。

SELECT SUM(player_a) AS sum_score
  FROM results
 WHERE (year = '2015' OR year = '2014')

EDIT 编辑

If you're wanting the maximum score by year, and then to sum that - this should be OK. 如果您想要逐年最高分,然后总结一下-这应该没问题。

SELECT MAX(max_sum_score) AS max_sum_score
  FROM ( SELECT year
              , SUM(player_a) AS max_sum_score
           FROM results
          WHERE (year = '2015' OR year = '2014')
          GROUP BY year 
       ) a

在此处输入图片说明

在此处输入图片说明

You may wanna use, 您可能想使用,

SELECT MAX(SUM_A),YEAR FROM (
    SELECT SUM( player_a ) as SUM_A, YEAR
    FROM results
GROUP BY YEAR
) A_SCORE;

This will do it in one go(obviously for one player). 这将一口气完成(显然是针对一名玩家)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM