MySQL:返回每个用户的最大值

时间:2017-07-06 14:26:58

标签: mysql sql

我有以下查询:

select username as User, floor((count(Goals)/2)-(if(sum(Loss)>0,1,0))) as Round, sum(Win) as Wins, sum(Goals) as Goals, sum(Loss) as Losses, campaign, Season, ((floor((count(Goals)/2)-(if(sum(Loss)>0,1,0)))*100000)+(sum(Win)*100)+sum(Goals)) as Score from
            (select u.username as username, s.campaignno as campaign, if(f.hometeamscore>f.awayteamscore,1,0) as Win, if(f.hometeamscore<f.awayteamscore,1,0) as Loss, f.hometeamscore as Goals, ss.seasonid as Season from straightred_fixture f, straightred_userselection s, auth_user u, straightred_season ss where s.fixtureid = f.fixtureid and s.teamselectionid = f.hometeamid and s.user_id = u.id union all
             select u.username as username, s.campaignno as campaign, if(f.awayteamscore>f.hometeamscore,1,0) as Win, if(f.awayteamscore<f.hometeamscore,1,0) as Loss, f.awayteamscore as Goals, ss.seasonid as Season from straightred_fixture f, straightred_userselection s, auth_user u, straightred_season ss where s.fixtureid = f.fixtureid and s.teamselectionid = f.awayteamid and s.user_id = u.id) t
            group by username, campaign, Season
            having Season = 1025
            order by Round DESC, Wins DESC, Goals DESC;

返回以下结果:

 +----------------------+-------+------+-------+--------+-----------+--------+--------+
| User                 | Round | Wins | Goals | Losses | campaign  | Season | Score  |
+----------------------+-------+------+-------+--------+-----------+--------+--------+
| shanu                |     9 |   12 |    36 |      1 | 354102525 |   1025 | 901236 |
| snookro              |     7 |   10 |    28 |      2 | 358102528 |   1025 | 701028 |
| tingeypa             |     6 |    9 |    19 |      2 |  35910257 |   1025 | 600919 |
| shaun                |     5 |    9 |    30 |      1 | 372102520 |   1025 | 500930 |
| shanu                |     5 |    9 |    25 |      2 |  35410256 |   1025 | 500925 |
| snookro              |     4 |    9 |    26 |      1 | 358102518 |   1025 | 400926 |
| chrakers             |     4 |    8 |    26 |      2 | 378102531 |   1025 | 400826 |
| freebet              |     4 |    8 |    24 |      1 | 375102524 |   1025 | 400824 |

结果表是正确的,但我只想要最大的&#34;分数&#34;为每个&#34;用户&#34;。你可以看到&#34; shanu&#34;和&#34; snookro&#34;表中有两个条目。

我试图用max函数包装得分,但MySQL告诉我&#34;无效使用组功能&#34;。

感谢任何帮助。

0 个答案:

没有答案
相关问题