본문 바로가기
Programming/LeetCode

[LeetCode] Consecutive Numbers (MySQL)

by 데이터현 2022. 3. 31.

https://leetcode.com/problems/consecutive-numbers/

 

Consecutive Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

row 3개 연속해서 등장하는 num을 찾는 문제.

 

1. Where IN 활용

select distinct Num as ConsecutiveNums from Logs
where (Id+1,Num) in (Select * from Logs) and (ID+2, Num) in (Select * from Logs)
;

 

2. self join 활용

SELECT distinct a.num ConsecutiveNums FROM
Logs a JOIN Logs b
on a.id = b.id - 1
JOIN logs c
on b.id = c.id - 1
WHERE a.num = b.num and b.num = c.num
;

댓글