【发布时间】:2013-01-04 19:02:45
【问题描述】:
使用以下 PostgreSQL 9.2.2 表:
id | parent_id
--------+----------
body | null
head | body
mouth | head
eye | head
tooth | mouth
tongue | mouth
sclera | eye
cornea | eye
我需要一个输出,其中列出每个非父母子女的所有直接间接父母,例如:
tooth mouth
tooth head
tooth body
tongue mouth
tongue head
tongue body
sclera eye
sclera head
sclera body
cornea eye
cornea head
cornea body
我尝试过搜索,我的结果只显示了使用 with-recursive 的单项查询,例如:
WITH RECURSIVE t AS (
SELECT parent_id, id
FROM item_tree
WHERE child_id = id
UNION ALL
SELECT it.parent_id, it.id
FROM item_tree it
JOIN t
ON it.child_id = t.parent_id
)
SELECT id, child_id
FROM t
我可以在外部编写一个循环,每次都替换id,但只能在 SQL 中完成吗?
@丹尼尔,
这是原始查询的输出:
id parent_id
------ ---------
cornea eye
cornea NULL
cornea head
cornea body
sclera eye
sclera head
sclera NULL
sclera body
tongue body
tongue head
tongue NULL
tongue mouth
tooth body
tooth head
tooth mouth
tooth NULL
然而,如果你用一个空过滤的选择语句将它括起来,即使你删除了内部的空过滤器,它也会给出所需的结果,如下所示:
select * from (
WITH RECURSIVE t(id,parent_id) AS (
select id,parent_id from item_tree i
UNION ALL
select t.id,i.parent_id from item_tree i JOIN t on i.id=t.parent_id
)
select * from t order by id
) t1 where parent_id is not null;
无论如何,我已经点击了复选标记,因为这可能是一个错误(我尝试通过 jdbc 和在 pgAdmin3 中运行两个查询,并具有相同的输出)
【问题讨论】:
标签: sql postgresql common-table-expression recursive-query