MySQL - Get longest chain of rows with a certain value -



MySQL - Get longest chain of rows with a certain value -

i can simplify table construction purposes of question following:

http://sqlfiddle.com/#!2/dcdd3/1

i want longest win streak each user (i.e. largest number of contiguous win=1 rows without win=0 row in between, returned in next format:

user_id | win_streak -------------------- 1 | 5 2 | 3 3 | 3

the current solution have of rows , build results in php foreach loop, can't help thinking there way in mysql.

you need column defines order of wins. assuming auto_increment column id:

select user_id, max(wins) longest_winning_streak ( select ugr.* , @winstreak := if(@prev_user = user_id, if(won = 1, @winstreak + 1, 0), 1) wins , @prev_user := user_id `user_game_results` ugr , (select @winstreak := 0, @prev_user := null) var_init order user_id, id ) sq grouping user_id see working live in sqlfiddle

your desired result not quite correct, user_id has 3 wins in row.

mysql

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

javascript - I need to update the text of a paragraph by inline edit -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -