sql 180. 连续出现的数字

编写一个 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 

 

posted @ 2019-06-18 11:51  大老虎打老虎  阅读(453)  评论(0编辑  收藏  举报