码迷,mamicode.com
首页 > 数据库 > 详细

sql 180. 连续出现的数字

时间:2019-06-18 12:27:51      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:编写   and   练习   sele   div   查找   多表   nbsp   sel   

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

+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

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

这道题解法练习一下多表连接的写法,虽然这种方式效率很低

select distinct logs.Num as ConsecutiveNums from logs as logs1,logs as logs2 ,logs where logs.id=logs1.id-1 and logs.id=logs2.id-2 and logs.Num=logs1.Num and logs.Num=logs2.Num 

 

sql 180. 连续出现的数字

标签:编写   and   练习   sele   div   查找   多表   nbsp   sel   

原文地址:https://www.cnblogs.com/wangshaowei/p/11044430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!