编写一个 SQL 查询,查找所有至少连续出现三次的数字。
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
这道题解法练习一下多表连接的写法,虽然这种方式效率很低
select distinct logs.Num as ConsecutiveNums from logs as logs1,logs as logs2 ,logs where logs.id=logs1.id-1 and logs.id=logs2.id-2 and logs.Num=logs1.Num and logs.Num=logs2.Num