180-Consecutive-Numbers

原题链接

0x0 题目详情

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+ | Id | Num | +----+-----+ | 1 |    1    | | 2 |    1    | | 3 |    1    | | 4 |    2    | | 5 |    1    | | 6 |    2    | | 7 |    2    | +----+-----+ 例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-------------------------+ | ConsecutiveNums | +-------------------------+ |               1               | +-------------------------+

0x1 解题思路

这道题也可以采用局部变量的方式求解。具体思路就是我们判断当前值和上一个值是否相等,如果相等,则对应的count计数加1。否则从1开始计数。

这道题我们要先完成对局部变量的赋值,将上述操作放在一个子查询里,在计算完所有的count后,首先使用where筛选出出现次数大于等于3的值,然后再使用distinctNum进行去重。

注意,这里有一个新的知识点:

使用>代替>=,因为>=会使索引失效!!!

0x2 代码实现

select distinct Num as ConsecutiveNums
from(
    select Num,
    case
        when @preValue=Num then @count:=@count+1
        when (@preValue:=Num) is not null then @count:=1
    end as `count` 
    from `Logs`,(select @count:=null,@preValue:=null) as init
) as countTable
#先执行where,再distinct
where `count`>2;

0x3 课后总结

养好用>而不用>=的习惯,因为>=会让索引失效。

Last updated

Was this helpful?