I have a table in a relational database in which I encode a tree using a technique known as a materialized path (also known as a Lineage column). That is, for each node in my tree, I have a row in the table, and for each row I have a row column called ancestry , where I store the path from the root of the node to node represented by this row.
Is it possible, and if so, how, select the rows in the orderd table by pre-order , that is, they should appear in the result set in the order that will be received by visiting the depth-first tree. I use MySQL - so there are no recursive queries and does not have the ltree extension .
For example, the tree, its table, and the selected ones are ordered in order:
1 SELECT * FROM nodes SELECT * FROM nodes ORDER BY ?depth_first_visit_order? | \ id | ancestry id | ancestry 2 3
Note. I am clearly interested in doing this on a materialized path encoding!
Related: What are the options for storing hierarchical data in a relational database?
clyfe source share