递归查询postgres12给出[42P19]错误

问题描述 投票:0回答:2

我正在使用一些父子玩具Simpsons数据在postgres postgres 12中学习递归。

这是创建语句:

create table Parent(
    parent varchar,
    child varchar
);

insert into Parent (parent, child) values
                                          ('homer', 'bart'),
                                          ('homer', 'lisa'),
                                          ('marge', 'bart'),
                                          ('marge', 'lisa'),
                                          ('abe', 'homer'),
                                          ('ape', 'abe');

现在,我想创建一个表,其中有一个列用于祖先,一个列用于后代,如下所示:

ancestor | descendant
----------------------
homer    | bart
homer    | lisa
marge    | bart
marge    | lisa
abe      | homer
ape      | abe
ape      | homer
abe      | bart
abe      | lisa
ape      | bart
ape      | lisa

我的解决方案产生此错误:

[42P19] ERROR: recursive reference to query "ancestor" must not appear more than once

这是我的代码:

with recursive
Ancestor(ancestor, descendant) as
    ((select parent, child from Parent)
     union
     (select a1.ancestor, a2.descendant
      from Ancestor a1, Ancestor a2
      where a1.descendant = a2.ancestor))
select *
from Ancestor;

我理解错误,但是我不理解如何在不使用Ancestor与自身的叉积创建中间表的情况下实现我想要的结果。

sql postgresql common-table-expression recursive-query
2个回答
2
投票

通常在递归CTE中,您联接到原始表,而不是自身联接到递归表。

如果执行此操作,则会得到所需的内容:

with recursive Ancestor(ancestor, descendant) as (
      select parent, child
      from Parent
      union all
      select a.ancestor, p.child
      from Ancestor a join
           parent p
           on a.descendant = p.parent
     )
select *
from Ancestor;

Here是db <>小提琴。


2
投票

您需要将父表加入CTE:

with recursive Ancestor as (
  select parent, child 
  from Parent
  where parent = 'abe'

  union

  select p.parent, p.child
  from parent p
     join ancestor a on a.child = p.parent
)
select *
from Ancestor;
© www.soinside.com 2019 - 2024. All rights reserved.