我在下面使用锦标赛系统。该表包含所有团队的注册长度。结果将是一个记分牌,总计所有球队的总分数。
我正在尝试将RANK()函数放入我的SQL中,但我现在卡住了。我希望从我的数据库中获得当前球队的得分。有人有任何想法吗?我正在使用MariaDB。
select team, sum(length) as totalScore
from
(SELECT t.*,
@num_in_group:=case when @team!=team then @num_in_group:=0 else @num_in_group:=@num_in_group+1 end as num_in_group,
@team:=team as t
FROM reg_catches t, (select @team:=-1, @num_in_group:=0) init
ORDER BY team asc, length desc) sub
WHERE sub.num_in_group<=4
GROUP BY team
ORDER BY totalScore DESC;
表
team length
-----------
26 70
25 70
25 95
25 98
25 100
25 100
25 100
25 122
当前输出
team totalScore
-- --
25 520
26 70
通缉输出
rank team totalScore
-- -- --
1 25 520
2 26 70
答案 0 :(得分:0)
SET @row = 0;
SELECT @row:=@row + 1 rank, a.team, a.total_score
FROM(SELECT team, sum(r.length) as total_score FROM reg_catches r GROUP BY
r.team) a;
试试上面的
答案 1 :(得分:0)
现在在Dickson的帮助下走到这一步,现在的问题是,排名似乎是基于团队ID而不是totalScore:O
SET @row = 0;
SELECT @row:=@row + 1 rank, team, sum(length) as totalScore
from
(SELECT t.*,
@num_in_group:=case when @team!=team then @num_in_group:=0 else @num_in_group:=@num_in_group+1 end as num_in_group,
@team:=team as t
FROM reg_catches t, (select @team:=-1, @num_in_group:=0) init
ORDER BY team asc, length desc) sub
WHERE sub.num_in_group<=4 and competition = "#COMPID" and disqualified = 0
GROUP BY team
ORDER BY totalScore DESC
当前输出
rank team totalScore
1 28381 479
58 28468 439
20 28412 436
25 28419 432
14 28404 427
5 28388 421
通缉将是
rank team totalScore
1 28381 479
2 28468 439
3 28412 436
4 28419 432
5 28404 427
6 28388 421