I have a tree using materialized path.
The table is similar to:
+-------------+--------+----------+-------+
| path | depth | children | score |
+-------------+--------+----------+-------+
| 0001 | 1 | 3 | 5 |
| 00010001 | 2 | 0 | -3 |
| 00010002 | 2 | 0 | 27 |
| 00010003 | 2 | 0 | 10 |
| 0002 | 1 | 2 | 12 |
| 00020001 | 2 | 0 | 0 |
| 00020002 | 2 | 1 | 3 |
| 00020002001 | 3 | 0 | 1 |
+-------------+--------+----------+-------+
I want to sort by the score
column while maintaining the tree structure.
What matters is children are beneath their parents.
+-------------+--------+----------+-------+
| path | depth | children | score |
+-------------+--------+----------+-------+
| 0002 | 1 | 2 | 12 |
| 00020002 | 2 | 1 | 3 |
| 00020002001 | 3 | 0 | 1 |
| 00020001 | 2 | 0 | 0 |
| 0001 | 1 | 3 | 5 |
| 00010002 | 2 | 0 | 27 |
| 00010003 | 2 | 0 | 10 |
| 00010001 | 2 | 0 | -3 |
+-------------+--------+----------+-------+
The path
column is only used in the database, so it doesn't have to be sequential.
The SQL I currently use to sort the tree so I can build it:
SELECT path, depth, children, score FROM mytable ORDER BY path ASC