1
我正在進行查詢以將數據拉出層次結構Postgresql遞歸CTE結果排序
例如,
CREATE table org (
id INT PRIMARY KEY,
name TEXT NOT NULL,
parent_id INT);
INSERT INTO org (id, name) VALUES (0, 'top');
INSERT INTO org (id, name, parent_id) VALUES (1, 'middle1', 0);
INSERT INTO org (id, name, parent_id) VALUES (2, 'middle2', 0);
INSERT INTO org (id, name, parent_id) VALUES (3, 'bottom3', 1);
WITH RECURSIVE parent_org (id, parent_id, name) AS (
SELECT id, parent_id, name
FROM org
WHERE id = 3
UNION ALL
SELECT o.id, o.parent_id, o.name
FROM org o, parent_org po
WHERE po.parent_id = o.id)
SELECT id, parent_id, name
FROM parent_org;
它按預期工作。
3 1 "bottom3"
1 0 "middle1"
0 "top"
它也返回數據的順序,我希望,這對我來說很有意義,它會做的,因爲結果將被發現的方式這一點。
問題是,我可以指望是這樣的順序嗎?