问:为什么没有179题
答:因为最近在更新数据库,179不是数据库,都是按照leetcode的模块来的,不会落下的大家放心
问题
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。
分析过程
1.查看表的所有内容
select * from logs;
2.创建2个表l1,l2看一下效果
select *
from
logs as l1,
logs as l2;
3.改成三个表l1,l2和l3
select *
from
logs as l1,
logs as l2,
logs as l3;
4.连续三个连续的数字
就是三个表的数值是一样的,并且id是连续的
select *
from
logs as l1,
logs as l2,
logs as l3
where
l1.Id = l2.Id - 1
and
l2.Id = l3.Id -1
and
l1.num = l2.Num
and
l2.Num = l3.Num;
5.按照要求的结果,去重,改列名
select distinct l1.Num as ConsecutiveNums
from
logs as l1,
logs as l2,
logs as 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 l1.Num as ConsecutiveNums
from
logs as l1,
logs as l2,
logs as l3
where
l1.Id = l2.Id - 1
and
l2.Id = l3.Id -1
and
l1.num = l2.Num
and
l2.Num = l3.Num;