簡體   English   中英

postgres中的connect_by_root等價物

[英]connect_by_root equivalent in postgres

如何在postgres中隱藏oracle的connect_by_root查詢。 例如:這是一個oracle查詢。

select fg_id, connect_by_root fg_id as fg_classifier_id
from fg
start with parent_fg_id is null
connect by prior fg_id = parent_fg_id 

您將使用遞歸公用表表達式,它只是通過遞歸級別“攜帶”根:

with recursive fg_tree as (
  select fg_id, 
         fg_id as fg_clasifier_id -- <<< this is the "root" 
  from fg
  where parent_fg_id is null -- <<< this is the "start with" part
  union all
  select c.fg_id, 
         p.fg_clasifier_id
  from fg c 
    join fg_tree p on p.fg_id = c.parent_fg_id -- <<< this is the "connect by" part
) 
select *
from fg_tree;

有關手冊中遞歸公用表表達式的更多詳細信息: http//www.postgresql.org/docs/current/static/queries-with.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM