2016-03-04 107 views
0

我正在创建Rock Papper Scisors游戏,我面临的问题是我需要根据3,5或7的最佳设置获胜者,并且要这样做我需要计算连续querys的数我的投注表很简单:获得连续的行mysql

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

请编辑您的问题并提供样品结果。 –

+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小提琴。

+0

没有工作,打印像grup –

+0

你不考虑如果以前的结果不同,它不会计数,例如,winneris:15,15,14是1为14 –

+0

@talles_jp。 。 。我修正了这一点。处理多个变量对于变量来说有点痛苦,所以可变键被连接在一起。 –

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 
+0

没有急着我需要这样的输出: 游戏|赢家|连续 145 | 15 | 2 –

+0

@talles_jp - 什么是“连续”?在你更新你的问题之前,我正在猜测你的输出... – sgeddes