SQL/프로그래머스

[SQL] with recursive

햄습햄 2021. 12. 17. 16:24

with recursive

 

with recursvie

--with recursive int_range(A)
with recursive int_range(hour) as (
    select 0
    union
    select int_range.hour + 1 from int_range where int_range.hour < 3
)
select * from int_range;


--with recursive int_range
with recursive int_range as (
    select 0 as hour
    union
    select int_range.hour + 1 from int_range where int_range.hour < 3
)
select * from int_range;


--필드가 2개 이상일 경우
--with recursive int_range(A, B)
with recursive int_range(hour, min) as (
    select 0, 11
    union
    select int_range.hour + 1, int_range.min from int_range where int_range.hour < 3
)
select * from int_range;


--with recursive int_range
with recursive int_range as (
    select 0 as hour, 11 as min
    union
    select int_range.hour + 1, int_range.min from int_range where int_range.hour < 3
)
select * from int_range;

--with recursive int_range(A)

1. 처음에 0 을 select 한다.

2. 그리고 where 절 조건에 맞을 때까지 1씩 늘린 값을 select 한다.

3. 최종적으로 0, 1, 2가 조회된다.



https://hyunmin1906.tistory.com/149

 

[MySQL] WITH 재귀(RECURSIVE) 쿼리 계층구조

주의 !! mysql 5.7 이하 미지원 ■ WITH RECURSIVE 문 1) 메모리 상에 가상의 테이블을 저장 2) 재귀 쿼리를 이용하여 실제로 테이블을 생성하거나 데이터삽입(INSERT)을 하지 않아도 가상 테이블을 생성할

hyunmin1906.tistory.com