欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库练习——leetcode(180):连续出现的数字

程序员文章站 2022-03-13 23:46:10
...

连续出现的数字

题目

数据库练习——leetcode(180):连续出现的数字

解析

首先一定要非常明确的是本题是要找连续出现的数字,连续
如何判断连续,通过ID来判断,
数据库练习——leetcode(180):连续出现的数字
步骤一:将表复制三份,分别命名为l1,l2,l3

select * from Logs as l1, Logs as l2, logs as l3

这个的效果为两个表相连接了,笛卡尔连接,一个ID对应另一个表的全部ID,效果如下:
数据库练习——leetcode(180):连续出现的数字
步骤二:找到连续的学号:

select * from Logs as l1, Logs as l2, logs as l3
where l1.id = l2.id-1 and l2.id = l3,id-1

效果如下
数据库练习——leetcode(180):连续出现的数字
步骤三:找到出现三以上相等的数字

select * from Logs as l1, Logs as l2, logs as l3
where l1.id = l2.id-1 and l2.id = l3.id-1
and l1.num = l2.num and l2.num =l3.num

步骤四:取出要找的结果

select distinct l1.Num as ConsecutiveNums from Logs as l1,Logs as l2,Logs as l3 where l1.id = l2.id-1 and l2.id = l3.id-1
and l1.Num = l2.Num and l2.Num = l3.Num

归纳:

select distinct l1.Num as ConsecutiveNums 
from Logs as l1,Logs as l2,Logs as l3 
where l1.id = l2.id-1 and l2.id = l3.id-1
and l1.Num = l2.Num and l2.Num = l3.Num

相关标签: # MySQL 大数据