Write a SQL query to find all numbers that appear at least three times consecutively.
+----+-----+
| 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.
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
本题也是对join使用的考察。与175. Combine Two Tables类似。join的用法在175. Combine Two Tables中有记录。
select
distinct l1.num as ConsecutiveNums
from logs l1
inner join logs l2
on l1.ID+1 = l2.ID
inner join logs l3
on l2.ID+1 = l3.ID
where l1.num = l2.num and l1.num = l3.num