在docs的示例中,可以使用WITH RECURSIVE
查询由具有父ID的行组成的树。
CREATE TABLE org(
name TEXT PRIMARY KEY,
boss TEXT REFERENCES org
);
提供的示例是后代查询(即为节点及其所有子节点执行此操作):
WITH RECURSIVE
works_for_alice(n) AS (
VALUES('Alice')
UNION
SELECT name FROM org, works_for_alice
WHERE org.boss=works_for_alice.n
)
SELECT avg(height) FROM org
WHERE org.name IN works_for_alice;
相反的例子是什么:具体来说:Alice up 的命令链直到Boss为空(即根节点)?与示例相反,我只需要一个结果列表,而不是进一步处理。
答案 0 :(得分:0)
WITH RECURSIVE
works_for_alice(n) AS (
VALUES('Alice')
UNION
SELECT boss FROM org WHERE name=n AND boss IS NOT NULL
)
SELECT avg(height) FROM org
WHERE org.name IN works_for_alice;