您的位置:首页 > 数据库

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

2020-03-24 19:16 573 查看

文章目录

连续出现的数字

题目

解析

首先一定要非常明确的是本题是要找连续出现的数字,连续
如何判断连续,通过ID来判断,

步骤一:将表复制三份,分别命名为l1,l2,l3

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

这个的效果为两个表相连接了,笛卡尔连接,一个ID对应另一个表的全部ID,效果如下:

步骤二:找到连续的学号:

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

效果如下

步骤三:找到出现三以上相等的数字

select * from Logs as l1, Logs as l2, logs as l3where 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
  • 点赞
  • 收藏
  • 分享
  • 文章举报
liuluTL 发布了33 篇原创文章 · 获赞 1 · 访问量 585 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: