我正在制作Rock Papper Scisors游戏,我面临的问题是我需要根据最佳3,5或7来设置胜利者,并且这样做我需要计算我的投注表很简单的连续查询数:
BO3
ID|GAME_ID|WINNER
1 |145 |15
2 |145 |14
3 |145 |15
4 |145 |15
15需要赢,我怎么能在mysql中计算它?
例如:
GAME_ID|WINNER|CONSECUTIVES
145|15 |2
非常感谢。
答案 0 :(得分:1)
我认为你需要变量:
select gameid, winner, max(rn)
from (select s.*,
(@rn := if(@gw = concat_ws(':', gameid, winner), @rn + 1,
if(@gw := concat_ws(':', gameid, winner), 1, 1)
)
) as rn
from scores s cross join
(select @gw := '', @rn := 0) params
order by s.id
) s
group by gameid, winner;
Here是一个SQL小提琴。
答案 1 :(得分:0)
也许是这样的:
select y.winner,
case when y2.cnt <= 3 then 'Best of 3'
when y2.cnt <= 5 then 'Best of 5'
when y2.cnt <= 7 then 'Best of 7'
end, count(*)
from yourtable y join (
select count(*) cnt, gameid
from yourtable
group by gameid) y2 on y.gameid = y2.gameid
group by y.gameid, y.winner, y2.cnt
having count(*) = 2 and y2.cnt <= 3 or
count(*) = 3 and y2.cnt <= 5 or
count(*) = 4 and y2.cnt <= 7