连续出现的数字(sql)
发布日期:2021-05-12 22:23:14 浏览次数:12 分类:精选文章

本文共 787 字,大约阅读时间需要 2 分钟。

表:Logs

+-------------+---------+

| Column Name | Type    |
+-------------+---------+
| id          | int     |
| num         | varchar |
+-------------+---------+
id 是这个表的主键。

 

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

返回的结果表中的数据可以按 任意顺序 排列。

 

查询结果格式如下面的例子所示:

 

Logs 表:

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

Result 表:

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
1 是唯一连续出现至少三次的数字。

来源:力扣(LeetCode)

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

我的解题:

select distinct(Num) as ConsecutiveNums from `Logs` as l where EXISTS(    (select l.Num     from ( select IF(count(Num)>=3,count(distinct(Num)),0) as dd     from `Logs` as l2     where l2.Id >= l.Id and l2.Id < l.Id+3 ) as ff     where ff.dd=1 ))

 

上一篇:mysql8新增特性(持续更新)
下一篇:今天面试发现好多sql基础知识已经忘了,这几天恶补几下

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年05月02日 15时36分16秒