I have a table in a relational database, in which I encode a tree using the technique known as Materialized path (also known as Lineage column). That is, for each node in my tree I have a row in the table, and for each row I have a string column named ancestry
where I store the path from the root node to the node represented by this row.
Is it possible, and if yes - how, to select the rows in the table orderd by preorder, that is they should appear in the result set in the order that would result by visiting the tree depth-first. I use MySQL - so no recursive queries and no ltree extension.
For example, a tree, it's table, and selected ordered by preorder:
1 SELECT * FROM nodes SELECT * FROM nodes ORDER BY ?depth_first_visit_order?
| \ id | ancestry id | ancestry
2 3 ------------- -------------
| | \ 1 | NULL 1 | NULL NOTE: I don't care about the
4 5 6 2 | 1 2 | 1 order of siblings!
| 3 | 1 4 | 1/2
7 4 | 1/2 3 | 1
5 | 1/3 5 | 1/3
6 | 1/3 7 | 1/3/5
7 | 1/3/5 6 | 1/3
Note: I am interested explicitly in doing this over a materialized path encoding!
Related: What are the options for storing hierarchical data in a relational database?