杨大伟在路上

大数据第33天—MySQL练习题4-杨大伟

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

展示效果:

ConsecutiveNums
1
1 Create table If Not Exists Logs (Id int, Num int);
2 
3 insert into Logs (Id, Num) values (1, 1);
4 insert into Logs (Id, Num) values (2, 1);
5 insert into Logs (Id, Num) values (3, 1);
6 insert into Logs (Id, Num) values (4, 2);
7 insert into Logs (Id, Num) values (5, 1);
8 insert into Logs (Id, Num) values (6, 2);
9 insert into Logs (Id, Num) values (7, 2);

 

最终SQL:

1 SELECT *
2 FROM
3     Logs l1,
4     Logs l2,
5     Logs l3
6 WHERE
7     l1.Id = l2.Id - 1 AND l1.Num = l2.Num
8     AND l2.Id = l3.Id - 1 AND l2.Num = l3.Num;

 

posted on 2020-08-07 18:54  浪子逆行  阅读(103)  评论(0编辑  收藏  举报

导航