您好我想选择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)
答案 0 :(得分:1)
好的答案很难过,我只需要改变
union all
到
union