SQL: Recursive Path

前端 未结 5 849
北海茫月
北海茫月 2021-02-10 23:57

Is it possible to create a \"tree resolver\" in SQL?

I have a table:

ID Name Parent
1  a
2  b    1
3  c    1
4  d    3

Now I want a SQL

5条回答
  •  悲&欢浪女
    2021-02-11 00:41

    Suppose we have a simple table called DLFolder with the following columns:

    | folderId | name | parentFolderId |
    

    In Oracle you can use the sys_connect_by_path operation.

    select fo.folderId as folder_id, sys_connect_by_path(fo.name, '/') as relname
    from DLFolder fo
    start with fo.parentFolderId=0
    connect by prior fo.folderId = fo.parentFolderId
    

    Will give the following result:

    /1020_Training_Material
    /1020_Training_Material/2000_IBBA
    /1020_Training_Material/2000_IBBA/5000_FR
    /1020_Training_Material/2000_IBBA/5050_NL
    

    See http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions164.htm

提交回复
热议问题