SQL: Recursive Path

前端 未结 5 846
北海茫月
北海茫月 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:40

    There are several different ways to represent a tree in an SQL database. I guess I don't know much, but I do know that Django Treebeard uses 3 different ways to do it. If you look at the documentation, it has short descriptions of each way:

    adjacency list -- what you're doing already

    materialized path -- article: http://www.dba-oracle.com/t_sql_patterns_trees.htm

    nested sets -- oh, here's wikipedia: http://en.wikipedia.org/wiki/Nested_set_model

提交回复
热议问题