力扣180. 连续出现的数字
https://leetcode-cn.com/problems/consecutive-numbers/
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
注意的点:
1、关键字 DISTINCT是防止返回重复元素(一个数字连续出现超过 3 次)
2、三表连接
3、条件查询,连续出现三次的条件
# Write your MySQL query statement below
select
distinct i1.num as ConsecutiveNums#关键字 DISTINCT是防止返回重复元素(一个数字连续出现超过 3 次)
from logs i1,logs i2,logs i3#三表连接
where i1.id=i2.id-1 #条件查询,连续出现三次的条件
and i2.id=i3.id-1
and i1.num=i2.num
and i2.num=i3.num;