数据库 连续出现的数字

54 阅读1分钟

180. 连续出现的数字 - 力扣(LeetCode)

思想

这类题是以找"连续出现N次"为主题的题目。

如果Id是连续的每次自增1的情况(题目说了Id是自增列),那么就会有下面这种情况:

image.png

我们发现Id为1,2,3的num值是呈连续出现状的。

因此我们在查询时就应该查询N个连续Id的Num值是否相同

code

select distinct a.num as ConsecutiveNums 
from 
     Logs as a,
     Logs as b,
     Logs as c
where 
  a.id=b.id-1
  and b.id=c.id-1
  and a.num=b.num
  and b.num=c.num;

image.png