连续出现的数字

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

Id Num
1 1
2 1
3 1
4 2
5 1
6 2
7 2
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。
ConsecutiveNums
1
解题思路
3表连接,分为 a ,b,c3张表
select distinct a.Num as ConsecutiveNums from Logs as a,Logs as b,Logs as c

条件,查找所有至少连续出现三次的数字。注意连续,也就是id是n,n-1,n-2
这三个id的值都必须相等
c表id假设为n,b表id为n-1,a表的id为b-1也就是n-2
where a.Num=b.Num and b.Num=c.Num and a.id=b.id-1 and b.id=c.id-1;

【连续出现的数字】完整sql
select distinct a.Num as ConsecutiveNums from Logs as a,Logs as b,Logs as c where a.Num=b.Num and b.Num=c.Num and a.id=b.id-1 and b.id=c.id-1;

    推荐阅读