https://school.programmers.co.kr/learn/courses/30/lessons/301651
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
생각 흐름
- 특정 세대 대장균이 아니라 모든 세대별 대장균에 대해서 데이터를 조회해야 하므로 재귀 쿼리 사용
- 자식이 없는 대장균을 거르기 위해 where 절 서브쿼리 사용
with recursive gen_data as (
select 1 as generation, id, parent_id
from ECOLI_DATA
where parent_id is null
union all
select generation+1 as generation, b.id, b.parent_id
from gen_data a inner join ECOLI_DATA b
on b.parent_id = a.id
)
select count(*) as count, generation
from gen_data
where id not in (
select distinct parent_id
from gen_data
where parent_id is not null
)
group by generation
order by generation
'코딩테스트 > MySQL' 카테고리의 다른 글
[ 프로그래머스 Level 3 ] 조건에 맞는 사용자와 총 거래금액 조회하기 (MySQL) (0) | 2025.03.07 |
---|---|
[프로그래머스 Level 5] 상품을 구매한 회원 비율 구하기 (MySQL) (0) | 2025.03.06 |
[프로그래머스 Level 4] FrontEnd 개발자 찾기 (MySQL) (0) | 2025.02.27 |
[프로그래머스 Level 4] 특정 세대의 대장균 찾기 (MySQL) (0) | 2025.02.19 |
[프로그래머스 Level 4] 입양 시각 구하기(2) (MySQL) (0) | 2025.02.16 |