postgre with递归查询组织路径

with递归查询组织路径

SELECT r.id,
    (array_to_string(
            array(
                    select name from (
                            with recursive rec as(
                                            select t.id,t.pid,t.name from t_org t where t.id = r.id
                                            union all
                                            select t1.id,t1.pid,t1.name from t_org t1 join rec r on r.pid = t1.id
                                    ) select id,name from rec order by id asc
                            ) as re 
                    ),'/')||'/') as "namepath"
    FROM t_org r ORDER BY id

 

posted @ 2019-12-19 16:11  君子笑而不语  阅读(483)  评论(1编辑  收藏  举报