본문 바로가기

개발이야기

오라클 계층형쿼리 mysql(maria db)로 변경

반응형
select  id,
        name,
        parent
from    (select * from RecursionTest
         order by parent, id) products_sorted,
        (select @pv := '3') initialisation
where   find_in_set(parent, @pv) > 0
and     @pv := concat(@pv, ',', id)


출처: http://bulkywebdeveloper.tistory.com/109 [건강한 코딩]




with recursive cte (id, name, parent) as
(
 select     id,
            name,
            parent
 from       recursiontest
 where      parent = 3
 union all
 select     r.id,
            r.name,
            r.parent
 from       recursiontest r
 inner join cte
         on r.parent = cte.id
)
 
select * from cte;



출처: http://bulkywebdeveloper.tistory.com/109 [건강한 코딩]