Explanation of consecutive numbers problems


  • 6
    N
    select distinct r.num  from 
        (select num,
            case when @last = num then @count:=@count+1
                when @last<>@last:=num then @count:=1
                end as n
            from Logs
        ) r ,(select @count:=0,@last:=(select num from Logs limit 0,1)) temp
    where r.n>=3
    

    In MYSQL , The execution order is : from ——> select ——> where ——>group by ——> order by ,

    in this case , we initialize two variables in from clause , then when our database engine scan the table row by row ,we change the two variables , when it meet the condition in where clause , we keep it ! That is the logic


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.