[LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
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)的更多相关文章
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数
mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...
- [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 ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- LeetCode——Consecutive Numbers
Description: Write a SQL query to find all numbers that appear at least three times consecutively. + ...
- [LeetCode] Department Highest Salary -- 数据库知识(mysql)
184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...
- 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. +----+ ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- python练习程序(c100经典例11)
题目: 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? a=1;b=1 print a,b, for i ...
- wxWidgets进度条
#include <wx/wx.h> #include <wx/progdlg.h> class myApp : public wxApp { public: bool OnI ...
- [转载] 问题解决:FFmpeg视频编解码库,无法解析的外部信号
在编译FFmpeg相关项目时,可能会出现: error LNK2019: 无法解析的外部符号 "int __cdecl avpicture_fill(struct AVPicture *,u ...
- mysql:mysql_query(): Unable to save result set
解决方式 方式1: 原因:数据表索引损坏 方案:使用repair table 表名; 方式2: 原因:打开表的数据量太大,内存不够. 方案:配置my.ini 调整内存能解决
- ORACLE RAC 下非缺省端口监听配置(listener.ora tnsnames.ora)
不论是单实例还是RAC,对于非缺省端口下(1521)的监听器,pmon进程不会将service/instance注册到监听器,即不会实现动态注册.与单实例相同,RAC非缺省端口的监听器也是通过设置参数 ...
- eclipse 工程加入ant以支持自动打war包
先在工程的根目录下建一个一builder.xml内容如下 <project basedir="." default="war" name="hb ...
- DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现
在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中,笔者阐述了一个防御状态机,它可用来抵御来自应用层的DDoS攻击,但是该状态机依赖一个能应对大量条目快速增删的ip黑白 ...
- delphi 中 $是什么意思 串口中使用
delphi 中 $是什么意思? 比如:$41----$5A 意识是26个字母, 可以用$来表示? $在delphi 中还可以怎么用?1.表示16进制,$41就是65,第一个字母的ASCII值 pro ...
- pdm 中怎么修改表的Name值时使Code值不变
修改方法:PowerDesign中的选项菜单里修改,在[Tool]-->[General Options]->[Dialog]->[Operating modes]->[Nam ...
- 将war包布署在本地tomcat上
1.把war包解压到..webapps目录下 2. 修改server.xml文件,在host节点中添加 <Context docBase="C:\Users\bai\Desktop\s ...