Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 896 Bytes

180. Consecutive Numbers.md

File metadata and controls

39 lines (34 loc) · 896 Bytes

180. Consecutive Numbers

Write 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               |
+-----------------+

题目大意

找出连续出现三次的数字

SQL

# Write your MySQL query statement below
SELECT DISTINCT a.Num AS ConsecutiveNums 
FROM `Logs` a LEFT JOIN `Logs` b ON a.id + 1 = b.id 
    LEFT JOIN `Logs` c on b.id + 1 = c.id
WHERE a.Num = b.num AND b.num = C.num

值得注意的是,表名logs好像是sql的保留字(输入后会变深蓝) 使用 "`" 来将其声明为表名