題目摘要
寫一個SQL查詢,用來尋找所有連續出現三次以上的Num
Id Num 1 1 2 1 3 1 4 2 5 1 6 2 7 2
例如,給定如上Logs
表,返回1
解法
# Write your MySQL query statement below
SELECT distinct(l1.Num) AS ConsecutiveNums
FROM Logs AS l1
INNER JOIN Logs AS l2
ON l1.id = l2.id - 1
AND l1.num = l2.num
INNER JOIN Logs AS l3
ON l2.id = l3.id - 1
AND l2.num = l3.num
注意
可問問題
原題
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.