Mysql为每个用户选择每组前N名分数
我正试图帮助一个哥们儿为他的高尔夫团体建立一个简单的网站。我尝试使用的数据库将存储每一轮完成的所有玩家的分数。还有两种类型的游戏将被播放和存储。示例如下:Mysql为每个用户选择每组前N名分数,mysql,sql,Mysql,Sql,我正试图帮助一个哥们儿为他的高尔夫团体建立一个简单的网站。我尝试使用的数据库将存储每一轮完成的所有玩家的分数。还有两种类型的游戏将被播放和存储。示例如下: ID Game Type Score 1 a 12 1 a 12 1 a 1 1 a 15 1 a
ID Game Type Score
1 a 12
1 a 12
1 a 1
1 a 15
1 a 15
1 b 12
1 b 5
1 b 10
1 b 12
1 b 5
1 b 10
2 a 6
2 a 9
2 a 1
2 a 3
2 a 2
2 b 8
2 b 10
2 b 15
2 b 3
2 b 12
我想做的是,为每个用户选择游戏类型A的前3个分数,并从游戏类型B中为每个用户选择前2个分数。
我需要选择产生什么,以便我可以对每个玩家的分数求和:
ID Game Type Score
1 a 12
1 a 15
1 a 15
1 b 12
1 b 12
2 a 6
2 a 9
2 a 3
2 b 15
2 b 12
我在bonCodigo网站上找到了一个类似的问题和解决方案。我将他的sql改为:
-- for top 2 sum by user by each day
SELECT userid, sum(score), type
FROM scores t1
where 3 >=
(SELECT count(*)
from scores t2
where t1.score <= t2.score
and t1.userid = t2.userid
and t1.type = t2.type
order by t2.score desc)
group by userid, type
;
-- for all two days top 2 sum by user
SELECT userid, sum(score)
FROM scores t1
where 3 >=
(SELECT count(*)
from scores t2
where t1.score <= t2.score
and t1.userid = t2.userid
and t1.type = t2.type
order by t2.score desc)
group by userid
;
我遇到的问题是,如果我想要前三名的分数,但第三名和第四名的分数相等,那么它将不会返回第三名的最高分数;只有前两名。附件是我一直试图使用的sqlfiddle。关于如何使用top 3的任何帮助,无论它们是否都相等!谢谢
因此,由于无法放弃这一点,我提出了一个怪物,使用一个存储过程来获得所需的输出,基本上是复制您在应用程序级别执行的操作,以获得相同的结果
create procedure leaderboard()
begin
declare uid int;
declare done int default 0;
declare cur1 cursor for select distinct userid from score;
declare continue handler for not found set done = 1;
drop table if exists score_temp;
create temporary table score_temp(userid int, score int, type int);
open cur1;
read_loop: LOOP
fetch cur1 into uid;
if done then
close cur1;
leave read_loop;
end if;
INNERBLOCK: begin
declare done2 int default 0;
declare i_userid int;
declare i_type int;
declare i_score int;
declare cur2 cursor for select * from score where userid = uid and type = 1 order by score desc limit 3;
declare continue handler for not found set done2 = 1;
open cur2;
inner_loop: LOOP
fetch cur2 into i_userid, i_score, i_type;
if done2 then
close cur2;
leave inner_loop;
end if;
insert into score_temp values (i_userid, i_score, i_type);
end loop inner_loop;
end INNERBLOCK;
INNERBLOCK: begin
declare done2 int default 0;
declare i_userid int;
declare i_type int;
declare i_score int;
declare cur2 cursor for select * from score where userid = uid and type = 2 order by score desc limit 2;
declare continue handler for not found set done2 = 1;
open cur2;
inner_loop: LOOP
fetch cur2 into i_userid, i_score, i_type;
if done2 then
close cur2;
leave inner_loop;
end if;
insert into score_temp values (i_userid, i_score, i_type);
end loop inner_loop;
end INNERBLOCK;
end loop read_loop;
select * from score_temp;
end
然后,要获得排行榜,您的查询是
呼叫排行榜
demo fiddle here:mysql是垃圾,因此您最好在应用程序中使用它。谢谢。我会记住这一点。我找到了一个非常接近的解决方案,但只是有点偏离了。我编辑了我的问题以进行反思。我不能评论另一个答案,因为我只是个傻瓜!希望有人能回答这个问题!好好了。基本上这很难,因为mysql不支持相关子查询中的限制,否则它会相对简单,我知道,对吧!理论上,这个问题不应该那么难,但在实践中似乎相当困难!哇,谢谢帕拉的帮助,并在这方面努力。我刚刚醒来,但稍后会看一看。干杯正如我在问题评论中所说的,这是mysql的一个缺陷。如果有什么方法可以不用手术,我不知道。这种逻辑方法不起作用,因为mysql没有这个功能。