Recursion 反转CTE的顺序

Recursion 反转CTE的顺序,recursion,mariadb,common-table-expression,Recursion,Mariadb,Common Table Expression,我有一个MariaDB 10.2.8数据库,用于存储特定根目录下所有文件的爬网结果。因此,一个文件(在文件表中)有一个父目录(在目录表中)。此父目录可能有自己的父目录,依此类推,直到目录爬网开始的原始点 因此,如果我从/home爬网,文件/home/tim/projects/foo/bar.py将有一个父目录foo,该目录将有一个父目录projects,依此类推/home(爬网的根目录)将有一个null父目录 我得到了以下递归CTE: with recursive tree as ( s

我有一个MariaDB 10.2.8数据库,用于存储特定根目录下所有文件的爬网结果。因此,一个文件(在
文件
表中)有一个父目录(在
目录
表中)。此父目录可能有自己的父目录,依此类推,直到目录爬网开始的原始点

因此,如果我从
/home
爬网,文件
/home/tim/projects/foo/bar.py
将有一个父目录
foo
,该目录将有一个父目录
projects
,依此类推
/home
(爬网的根目录)将有一个
null
父目录

我得到了以下递归CTE:

with recursive tree as (
    select id, name, parent from directory where id = 
    (select parent from file where id = @FileID)
    union 
    select d.id, d.name, d.parent from directory d, tree t
    where t.parent = d.id
) select name from tree;
按顺序返回结果,其中
@FileID
是文件的主键。e、 g

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.2.8-MariaDB-10.2.8+maria~jessie-log mariadb.org binary distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use inventory;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [inventory]> with recursive tree as (
    -> select id, name, parent from directory where id =
    -> (select parent from file where id = 3790)
    -> union
    -> select d.id, d.name, d.parent from directory d, tree t
    -> where t.parent = d.id
    -> ) select name from tree;
+----------+
| name     |
+----------+
| b8       |
| objects  |
| .git     |
| fresnel  |
| Projects |
| metatron |
+----------+
6 rows in set (0.00 sec)

MariaDB [inventory]> Bye
tim@merlin:~$
因此在本例中,文件ID 3790对应于目录
/metatron/Projects/fresnel/.git/objects/b8
(当然,
/metatron
是爬网的根目录)中的一个文件

是否有一种可靠的方法可以反转输出的顺序(因为我想将其连接在一起以生成完整的路径)。我可以
按id排序
,但这并不可靠,因为即使我知道,在这种情况下,孩子的id会比他们的父母高。我不能保证在我想使用CTE的任何情况下都会如此

(
     your-current-query
) ORDER BY ...;
(如果您遇到问题,请将
选择…
也粘在前面