leetcode-180-Consecutive-Numbers

描述


Write a SQL query to find all numbers that appear at least three times consecutively.

1
2
3
4
5
6
7
8
9
10
11
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

分析


找出 Id 连续且值相同的 Num

解决方案(MySQL)


1
2
3
4
# Write your MySQL query statement below
SELECT DISTINCT(t1.Num) as ConsecutiveNums FROM Logs t1, Logs t2, Logs t3
WHERE t1.Id=t2.Id-1 AND t2.Id=t3.Id-1 AND
t1.Num=t2.Num AND t2.Num=t3.Num

题目来源