Suppose I have a table of objects structured in a hierarchy:
A
|--B
|--C
| +--D
+--E
They are stored in a "parent-child" table thus:
parent child
A B
A C
C D
A E
How do I query this to get the structure defined above? I think I need something that produces info like this:
object full_path
A NULL
B A
C A
D A.C
E A
I cannot figure out how to do the objects nested more than one level deep. It feels like I might need to iterate over the table (no idea if this is possible in SQL), or otherwise use some kind of query I've never encountered before.
Additional Info:
- A need not be the only orphan object.
- Children may have multiple parents BUT for now I'm happy with an answer to whichever scenario is easier to solve. I can abide converting a multi-parent structure to a single parent with a simple
GROUP BY
and MIN
statement.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…