【问题标题】:connect_by_root equivalent in postgrespostgres 中的 connect_by_root 等效项
【发布时间】:2014-03-10 17:31:11
【问题描述】:

如何在 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 

【问题讨论】:

    标签: sql oracle postgresql


    【解决方案1】:

    您将使用递归公用表表达式,它只是通过递归级别“携带”根:

    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

    【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多