leetcode--180--连续出现的数字

2020-03-28  本文已影响0人  minningl

题目:
编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+

链接:https://leetcode-cn.com/problems/consecutive-numbers

思路:这道题主要是考察sql中重复使用同一张表并且利用其id来获取连续编号数字的特性

SQL如下:

select distinct(A.Num) as ConsecutiveNums
  from Logs as A,
       Logs as B,
       Logs as C
 where A.Num=B.Num
   and B.Num=C.Num
   and A.Id=B.Id-1
   and B.Id = C.Id-1
上一篇下一篇

猜你喜欢

热点阅读