MySQL – Get the Largest Count of Consecutive Values

MySQLmysql-5.7

I have this table that records win and lose. Let us say this is my table

create table test(
  col1 int not null
)Engine=InnoDB;

it has 10 rows :

0,1,1,1,0,1,1,0,0,1

1=Win and 0=Lose. I want to get max count of the consecutive win and lose. For this example, the max count for consecutive win is 3 and for lose is 2. How can I get my desired result? I'm not sure where to start with this one. Thanks in advance.

EDIT

create table t1 (
 colid int not null auto_increment,
 col1 int,

 primary key(colid)
);

insert into t1(col1) values (0);
insert into t1(col1) values (1);
insert into t1(col1) values (1);
insert into t1(col1) values (1);
insert into t1(col1) values (0);
insert into t1(col1) values (1);
insert into t1(col1) values (1);  
insert into t1(col1) values (0);
insert into t1(col1) values (0);
insert into t1(col1) values (1);


select 
a.col1,
max(a.rn)
from (

        select 
        x.col1,
        if (@prev = col1 ,@current := @current + 1,@current := 1 ) rn, @prev := col1
      from t1 x , (select @prev := - 1 , @current := 1) v  
      order by x.colid
      ) a
group by a.col1

this one works the way I see the results.. Is there anything I should consider? I only have knowledge on basic joins. I haven't fully understand the query as of now..

Best Answer

create table t1 (
col1 int 
);

insert into t1 values (0);
insert into t1 values (1);
insert into t1 values (1);
insert into t1 values (1);
insert into t1 values (0);
insert into t1 values (1);
insert into t1 values (1);  
insert into t1 values (0);
insert into t1 values (0);
insert into t1 values (1);

select t.col1,
   max(t.rn)
  from (
select t1.col1,
if (@prev = col1 ,@current := @current + 1,@current := 1 ) rn,
   @prev := col1
 from t1,(select @prev := - 1 ,
              @current := 1) v  
 ) t
 group by t.col1;

DB fiddle