If you store a tree structure in a table, you need to deposit two field IDs and ParentID in the tables, indicating who the parent of each record is.
Table:treetable
Id |
ParentID |
Field 3 |
...... |
1 |
Null |
A |
...... |
2 |
1 |
B |
...... |
3 |
2 |
D |
...... |
Select ... can be used in Oracle. From ... Start with (starting node) ... CONNECT by ... PRIOR (indicates previous record) ... (Order siblings by levels) clause implements a recursive query tree structure.
1. Traverse the child node from the root node.: (A whole tree)
SELECT * from treetable start and id=1 connect by prior Id=parentid (prior represents previous record)
2. Traced from one leaf to the root node: (one branch of the tree)
SELECT * from treetable start and id=3 connect by prior Parentid=id (prior represents previous record)
3. The level keyword can be queried.
Select *,level from treetable start and id=1 connect by prior Id=parentid (prior represents the previous record)