Q:
编写一个 SQL 查询,查找所有至少连续出现三次的数字
Demo_table:
A:
select distinct
l1.Num as ConsecutiveNums
from
Demo_table l1,
Demo_table l2,
Demo_table l3
where
l1.Id = l2.Id - 1
And l2.Id = l3.Id -1
And l1.Num = l2.Num
And l2.Num = l3,Num
;
释:
- 通过连续的
Id -1
可确保他们的连续性 - 然后一一比对相等