1. 题目名称   Consecutive Numbers

2 .题目地址 https://leetcode.com/problems/consecutive-numbers/

3. 题目内容 写一个SQL,查处表Logs中连续出现至少3次的数字:(考察的知识点主要是多个表之间的连接)

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

比如上表中,写出的结果是1.

4. 解题思路

解法一:直接使用一套SELECT - FROM -WHERE语句:

SELECT DISTINCT L1.Num FROM Logs L1, Logs L2, Logs L3
WHERE (L1.Id = L2.Id + 1 AND L1.Num = L2.Num) AND
(L1.Id = L3.Id + 2 AND L1.Num = L3.Num);

理论上正确,但是在leetcode上运行结果不通过,还未知什么原因。看过discuss的讨论后,加入返回的列的名字ConsecutiveNums后AC,但是郁闷的是题目没有给出要返回的列的名字。

解法二:可以使用JOIN句子完成相同的功能

SELECT DISTINCT L1.Num FROM Logs L1
JOIN Logs L2 ON L1.Id + 1 = L2.Id
JOIN Logs L3 ON L1.Id + 2 = L3.Id
WHERE L1.Num = L2.Num AND L1.Num = L3.Num
ORDER BY L1.Num

运行结果不通过。在加入 返回的列名ConsecutiveNums 后,运行通过,不过效率没有第一种方法高。

解法三:上面两种方法可以用于找到至少三次连续出现的数字,如果将连续出现的数字扩展到N个,按照上面思路写出的SQL语句就会比较长。因此可以用下面这种方式来查询:

SELECT DISTINCT Num
FROM (
SELECT Num,
CASE
WHEN @prev = Num THEN @count := @count + 1
WHEN (@prev := Num) IS NOT NULL THEN @count := 1
END CNT
FROM Logs, (SELECT @prev := NULL) X
ORDER BY Id
) AS A
WHERE A.CNT >= 3

将最后一行的3改为N,即可用于查询至少N次连续出现的数字。

[LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)的更多相关文章

  1. [LeetCode] Consecutive Numbers 连续的数字

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

  2. Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数

    mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...

  3. [LeetCode] Rank Scores -- 数据库知识(mysql)

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  4. [LeetCode] Lexicographical Numbers 字典顺序的数字

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  5. LeetCode——Consecutive Numbers

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

  6. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  7. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  8. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

随机推荐

  1. Android telnet RPi 2B

    /************************************************************************* * Android telnet RPi 2B * ...

  2. open行情

    日k线 只能取8年 1分钟K线 只能取五天 前复权K线出现负数的股价 后复权K线会出现上千的股价

  3. scala学习笔记(4):占位符

    scala 中占位符的用法 1.作为“通配符”,类似Java中的*.如import scala.math._ 2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = s ...

  4. C# 编写Windows Service(windows服务程序)【转载】

    [转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...

  5. SAS9.4 安装注意事项

    OS:Windows Server 2012 R2 SAS:9.4 TS1M1 SAS9.4的安装需要注意的地方: 一. 注意开启Shortname(装了N遍,最容易忘记的地方) SAS安装使用Sho ...

  6. poj 2127 LCIS 带路径输出

    这个题   用一维 为什么错了: 因为 用一维 dp 方程肯定也是一维:但是有没有想,第 i 个字符更新了 j 位置的最优结果,然后 k 字符又一次更新了  j 位置的最优值,然后  我的结果是  i ...

  7. SeuRain的归来

    不知不觉二十载寒窗苦读要结束了,还没有到回顾过去的时候.马上进入研三了,现在要努力加油了.还记得曾经的那个在凌晨两点奋战的宇么?归来吧!

  8. 查看造成等待事件的具体SQL语句

    先查看存在的等待事件:col event for a40col WAIT_CLASS format a20select sid,WAIT_CLASS,event,p1,p2,p3,WAIT_TIME, ...

  9. background-size background-positon合并的写法

    background:url('../../image/banner/banner1.jpg') #fff no-repeat 5px center/50px 50px; "/"前 ...

  10. 通过文件流stream下载文件

    public ActionResult ShowLocalizedXML(int id) { string orderName = ""; string xmlString = G ...