cjql / algorithm

https://cjql.github.io/algorithm/
1 stars 1 forks source link

sql: consecutive-numbers #14

Open cjql opened 4 years ago

cjql commented 4 years ago

https://leetcode.com/problems/consecutive-numbers/

Select DISTINCT l1.Num AS ConsecutiveNums
from Logs l1, Logs l2, Logs l3 
where l1.Id=l2.Id-1 and l2.Id=l3.Id-1 
and l1.Num=l2.Num and l2.Num=l3.Num
select DISTINCT num AS ConsecutiveNums 
FROM
(select num,
    case 
        when @record = num then @count:=@count+1
        when @record <> @record:=num then @count:=1 end as n
    from 
        Logs ,(select @count:=0,@record:=(SELECT num from Logs limit 0,1)) r
) a
where a.n>=3