反向SELECT查询结果
Reverse SELECT query result
我有递归查询,其中 returns 分层模型中的某些行。
种类:
files_array := ARRAY
(WITH RECURSIVE files_to_parent AS (
SELECT FileID, Name, ParentID
FROM File
WHERE FileID = file_id
UNION ALL
SELECT F.FileID, F.Name, F.ParentID
FROM files_to_parent ftp, File F
WHERE F.FileID = FTP.ParentID
)
SELECT Name FROM files_to_parent);
如何反转 SELECT 查询的结果?
PS:我无法按 ID 排序,parent 的 ID 可能比 child 中的 ID 多或少。
您必须添加明确的 ORDER BY
子句才能获得特定顺序:
WITH RECURSIVE files_to_parent AS (
SELECT FileID, Name, ParentID,
1 AS recursion_depth
FROM File
WHERE FileID = file_id
UNION ALL
SELECT F.FileID, F.Name, F.ParentID,
ftp.recursion_depth + 1
FROM files_to_parent ftp
JOIN File F ON F.FileID = FTP.ParentID
)
SELECT Name
FROM files_to_parent
ORDER BY recursion_depth DESC;
我有递归查询,其中 returns 分层模型中的某些行。 种类:
files_array := ARRAY
(WITH RECURSIVE files_to_parent AS (
SELECT FileID, Name, ParentID
FROM File
WHERE FileID = file_id
UNION ALL
SELECT F.FileID, F.Name, F.ParentID
FROM files_to_parent ftp, File F
WHERE F.FileID = FTP.ParentID
)
SELECT Name FROM files_to_parent);
如何反转 SELECT 查询的结果?
PS:我无法按 ID 排序,parent 的 ID 可能比 child 中的 ID 多或少。
您必须添加明确的 ORDER BY
子句才能获得特定顺序:
WITH RECURSIVE files_to_parent AS (
SELECT FileID, Name, ParentID,
1 AS recursion_depth
FROM File
WHERE FileID = file_id
UNION ALL
SELECT F.FileID, F.Name, F.ParentID,
ftp.recursion_depth + 1
FROM files_to_parent ftp
JOIN File F ON F.FileID = FTP.ParentID
)
SELECT Name
FROM files_to_parent
ORDER BY recursion_depth DESC;