博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Consecutive Numbers leetcode
阅读量:6335 次
发布时间:2019-06-22

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

hot3.png

Consecutive NumbersWrite 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               |+-----------------+solution:用普通的关联select distinct a.Num as ConsecutiveNums from Logs a,logs b,Logs cwhere a.Num = b.Num and b.Num = c.Numand a.Id = b.Id-1 and b.Id = c.Id-1与上面类似的 我们换成 Join 关联select distinct a.Num as ConsecutiveNums from Logs ajoin logs bJoin Logs cwhere a.Num = b.Num and b.Num = c.Numand a.Id = b.Id-1 and b.Id = c.Id-1当然 这边的条件 写在 on关联还是 放在where中 结果是一样实际执行计划 不知道有什么变化SELECT DISTINCT l1.Num FROM Logs aJOIN Logs b ON a.Id = b.Id - 1JOIN Logs  c ON a.Id = c.Id - 2WHERE a.Num = b.Num AND b.Num = c.Num;然后又是设置两个变量的写法SELECT DISTINCT Num as ConsecutiveNums FROM (SELECT Num, @count := IF(@pre = Num, @count + 1, 1) AS n, @pre := NumFROM Logs, (SELECT @count := 0, @pre := -1) AS init) AS t WHERE t.n >= 3;

转载于:https://my.oschina.net/u/2277632/blog/1593555

你可能感兴趣的文章
wampServer连接oracle
查看>>
CentOS 6.5下编译安装新版LNMP
查看>>
Android Picasso
查看>>
top命令
查看>>
javascript的作用域
查看>>
新形势下初创B2B行业网站如何经营
查看>>
初心大陆-----python宝典 第五章之列表
查看>>
java基础学习2
查看>>
sysbench使用笔记
查看>>
有关电子商务信息的介绍
查看>>
NFC·(近距离无线通讯技术)
查看>>
多线程基础(三)NSThread基础
查看>>
PHP的学习--Traits新特性
查看>>
ubuntu下,py2,py3共存,/usr/bin/python: No module named virtualenvwrapper错误解决方法
查看>>
Ext.form.field.Number numberfield
查看>>
Linux文件夹分析
查看>>
解决部分月份绩效无法显示的问题:timestamp\union al\autocommit等的用法
查看>>
nginx 域名跳转 Nginx跳转自动到带www域名规则配置、nginx多域名向主域名跳转
查看>>
man openstack >>1.txt
查看>>
linux几大服务器版本大比拼
查看>>