简体   繁体   English

SQL Server中父级和子级关系的递归CTE

[英]Recursive CTE for parent and child relationship in SQL Server

Ok so I've seen a lot of examples online of this but I can't get it to work with my table. 好的,所以我在网上看到了很多示例,但是我无法使其与我的桌子一起使用。

Table: product 表:产品
Columns: parent_product_id, child_product_id 列:parent_product_id,child_product_id

If parent_product_id = child_product_id then the parent_product_id does not have a parent. 如果parent_product_id = child_product_id则parent_product_id没有父母。

A child_product_id could be the parent of another record. child_product_id可以是另一条记录的父项。

I tried to do this but it's taking forever for me to see the hierarchy of parent_product_id = 392193 我试图这样做,但花了我很长时间才能看到parent_product_id = 392193的层次结构

;with  parents
        as ( 
            select  child_product_id,
                    parent_product_id
            from    product
            where   parent_product_id = child_product_id
            union all 
            select  e.child_product_id,
                    e.parent_product_id
            from    product e
            inner join parents m
            on      e.parent_product_id = m.child_product_id)
  select  *
  from    parents  
  where parents.parent_product_id = 392193
  option (maxrecursion 0)

Can anyone give me a hand? 有人可以帮我吗?

You could move the start condition inside the CTE: 您可以在CTE中移动开始条件:

; with  parents as
        ( 
        select  child_product_id
        ,       parent_product_id
        from    product
        where   child_product_id = 392193
                and parent_product_id = 392193
        union all 
        select  e.child_product_id
        ,       e.parent_product_id
        from    parents m
        join    product e
        on      e.parent_product_id = m.child_product_id
        )
select  *
from    parents  
option  (maxrecursion 0)

An index on (parent_product_id, child_product_id) would help. 索引(parent_product_id, child_product_id)会有所帮助。

Usually, a child record refers to the primary key of its parent record. 通常,子记录是指其父记录的主键。 In your case, there's something unusual going on, with the parent having a "child_product_id". 在您的情况下,发生了一些异常情况,其中父项具有“ child_product_id”。 Some more information on this construct would clarify your question. 有关此构造的更多信息将阐明您的问题。

try this: 尝试这个:

;with  parents
        as ( 
            select  parent_product_id,
                    child_product_id      
            from    product
            where   parent_product_id = child_product_id
            union all 
            select  m.parent_product_id, --this should be parent of top level
                    e.child_product_id   
            from    product e
            inner join parents m
            on      e.parent_product_id = m.child_product_id WHERE e.parent_product_id != e.child_product_id
            )
  select  *
  from    parents  
  where parents.parent_product_id = 392193
  option (maxrecursion 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM