★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10152346.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

SQL架构:

1 Create table If Not Exists Logs (Id int, Num int)
2 Truncate table Logs
3 insert into Logs (Id, Num) values ('1', '1')
4 insert into Logs (Id, Num) values ('2', '1')
5 insert into Logs (Id, Num) values ('3', '1')
6 insert into Logs (Id, Num) values ('4', '2')
7 insert into Logs (Id, Num) values ('5', '1')
8 insert into Logs (Id, Num) values ('6', '2')
9 insert into Logs (Id, Num) values ('7', '2')


Write a SQL query to find all numbers that appear at least three times consecutively.

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

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

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

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

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

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

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

198ms
 1 # Write your MySQL query statement below
 2
 3
 4 # SELECT
 5 #     DISTINCT l1.Num ConsecutiveNums
 6 # FROM
 7 #     logs l1,
 8 #     logs l2,
 9 #     logs l3
10 # WHERE
11 #     l1.Id = l2.Id - 1 AND
12 #     l2.Id = l3.Id - 1 AND
13 #     l1.Num = l2.Num AND
14 #     l2.Num = l3.Num
15
16 select DISTINCT num  AS "ConsecutiveNums" FROM
17 (select num,
18     case
19         when @record = num then @count:=@count+1
20         when @record <> @record:=num then @count:=1 end as n
21     from
22         Logs ,(select @count:=0,@record:=(SELECT num from Logs limit 0,1)) r
23 ) a
24 where a.n>=3


200ms

 1 # Write your MySQL query statement below
 2 /*
 3 Select DISTINCT l1.Num As ConsecutiveNums from Logs l1, Logs l2, Logs l3
 4 where l1.Id = l2.Id - 1 and l2.Id = l3.Id - 1
 5 and l1.Num = l2.Num and l2.Num = l3.Num
 6 */
 7 select distinct(Num) as ConsecutiveNums
 8 from (
 9     select
10     Num,
11     @counter := if(@prev = Num, @counter + 1, 1) as cnt,
12     @prev := Num
13     from Logs y, (select @counter := 1, @prev := NULL) as tmp
14     ) as counts
15 where cnt >= 3;


202ms

 1 # Write your MySQL query statement below
 2 #select Num as ConsecutiveNums from Logs where
 3
 4 #select Num, count(Id) as counter from Logs group by Num where counter >= 3
 5
 6 select distinct Num as ConsecutiveNums from
 7     (select
 8         Num,
 9         @count := if(@prev = (@prev := Num), @count + 1, 1) as counter
10     from
11         Logs,
12         (select @prev := -1, @count := 1) as temp
13      ) as result
14 where counter >= 3;


237ms

1 # Write your MySQL query statement below
2 SELECT distinct num as ConsecutiveNums FROM(
3 SELECT id, num,
4 @pre := @cur,
5 @cur := num,
6 @rep_ct := IF(@pre = @cur, @rep_ct + 1, 1) as rep_ct
7 FROM `Logs` l, (SELECT @pre := null, @cur := 0, @rep_ct := 1) init
8 ) temp WHERE rep_ct >= 3


262ms

 1 # Write your MySQL query statement below
 2 SELECT x.Num as ConsecutiveNums FROM
 3 (SELECT y.Num , SUM(INDEXS) FROM
 4     (SELECT Num,
 5     CASE WHEN @preNum = t.Num then @index else @index := 1
 6     END AS INDEXS,
 7     @index := @index+1,
 8     @preNum := t.Num
 9     FROM Logs t,
10     (SELECT@preNum := null, @index := 1) dse)y
11 WHERE y.INDEXS > 2
12 GROUP BY y.Num)x

转载于:https://www.cnblogs.com/strengthen/p/10152346.html

[SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers相关推荐

  1. leetcode180. 连续出现的数字(SQL)

    编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +----+-----+ | 1  |  1  | | 2  |  1  | | 3  | ...

  2. Divide a list of numbers into group of consecutive numbers

    //Divide a list of numbers into group of consecutive numbers but their original order should be pres ...

  3. LeetCode 829. Consecutive Numbers Sum--笔试题--C++解法

    LeetCode 829. Consecutive Numbers Sum–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题 ...

  4. 解决html连续字符或数字换行的问题

    解决html连续字符或数字换行的问题 参考文章: (1)解决html连续字符或数字换行的问题 (2)https://www.cnblogs.com/plBlog/p/11428177.html 备忘一 ...

  5. Java黑皮书课后题第8章:**8.19(模式识别:四个连续相等的数)编写下面的方法,测试一个二维数组是否有四个连续相等的数字(水平、垂直、对角线方向都可以)。编写一个测试程序,提示用户输入一个数组

    **8.19(模式识别:四个连续相等的数)编写下面的方法,测试一个二维数组是否有四个连续相等的数字(水平.垂直.对角线方向都可以).编写一个测试程序,提示用户输入一个数组 题目 题目描述与运行实例 破 ...

  6. SQL Server 2008 R2——使用数字辅助表(master..spt_values)实现用计数字段对记录进行重复显示...

    SQL Server 2008 R2--使用数字辅助表(master..spt_values)实现用计数字段对记录进行重复显示 原文:SQL Server 2008 R2--使用数字辅助表(maste ...

  7. js 数字递增递减_js验证连续两位数字递增或递减和连续三位数字相同

    验证 function isPassword(){//连续三位数字相同 var str = $("#testid").val(); //var patrn=/(.)*(.)\2{2 ...

  8. LeetCode数据库 180. 连续出现的数字

    180. 连续出现的数字 SELECT DISTINCT L1.Num AS ConsecutiveNums FROM Logs AS l1, Logs AS l2, Logs AS l3 WHERE ...

  9. sql查询非11位非数字_非生产环境SQL查询性能调优技巧

    sql查询非11位非数字 It is a common misconception that you need real production data, or production like dat ...

最新文章

  1. 2020-11-17(补码的非)
  2. qt creator创建cmake构建的程序,无法启动调试(点左下角运行不出结果 No executable specified.)
  3. 单例-初始化动作只执行一次
  4. Android使用Intent实现拨打电话的动作
  5. 牛客-Forsaken喜欢独一无二的树【并查集,最小生成树】
  6. 深入浅出JVM-内存模型
  7. linux非权限安装bioperl,bioperl的安装
  8. 华为背锅?微博大V质疑华为P30 Pro拍月亮造假 公司称误导观众已开除
  9. 金蝶kis云触发器解决审核和反审核的问题
  10. 通俗易懂!视觉slam第三部分——slam数学表示
  11. SAX EntityResolver 的作用
  12. MBR分区表格式与GPT分区表格式简介
  13. Homography拓展(含与平面法向量相关的情况)
  14. ssm基于web的教务管理系统毕业设计源码261620
  15. docker启动redis指定redis.conf参数
  16. 程序猿段子_程序员的那些段子
  17. EfficientNET_V1
  18. mysql 8.XXX zip版的安装使用
  19. 发送打印任务后不打印
  20. EMAC和PHY层之间的关系以及在通信架构划分情况

热门文章

  1. Redis--安装与配置(Linux与Windows)
  2. 【模拟】蓝桥20:蛇形填数
  3. 【数据结构和算法笔记】用c和c++分别实现二叉搜索树
  4. LeetCode 701 二叉搜索树中的插入操作
  5. EF Core 的Startup配置自动创建数据库
  6. css将空的div撑开,如何使用css将空的浮动div伸展到可用的全高度?
  7. POI设置单元格格式
  8. Linux系统根据端口号查找项目路径
  9. meta refresh 刷新
  10. 【编辑器】VSCode项目管理器——Project Manager