带递归的PostgreSQL - >防止死锁/递归

时间:2014-08-04 15:08:36

标签: postgresql

您好我想选择postgres递归的对象。但我不能保证数据库中的每个数据都是正确的。所以我并不是不可能将对象本身作为父ID。

当对象已经存在时是否可以停止?我尝试了什么:

with recursive sumthis(myobjectid,parent_myobjectid) as (
    select myobjectid, parent_myobjectid
    from myobjectentity
    where myobjectid = 243358
    union all
    select C.myobjectid, C.parent_myobjectid
    from sumthis P
    inner join myobjectentity C on P.myobjectid = C.parent_myobjectid AND (P.myobjectid NOT IN (SELECT myobjectid FROM sumthis))

)

SELECT * FROM myobjectentity WHERE myobjectid IN (select myobjectid from sumthis)

with recursive sumthis(myobjectid,parent_myobjectid) as (
    select myobjectid, parent_myobjectid
    from myobjectentity
    where myobjectid = 243358
    union all
    select C.myobjectid, C.parent_myobjectid
    from sumthis P
    inner join myobjectentity C on P.myobjectid = C.parent_myobjectid
    Limit 100

)

SELECT * FROM myobjectentity WHERE myobjectid IN (select myobjectid from sumthis)

1 个答案:

答案 0 :(得分:1)

好的答案很难过,我只需要改变

union all 

union