【发布时间】:2017-12-27 21:42:26
【问题描述】:
好的,我在网上看到了很多这样的例子,但我无法让它与我的表一起使用。
表:产品
列:parent_product_id、child_product_id
如果 parent_product_id = child_product_id 则 parent_product_id 没有父代。
child_product_id 可以是另一条记录的父级。
我尝试这样做,但我花了很长时间才能看到 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)
谁能帮帮我?
【问题讨论】:
标签: sql sql-server recursion common-table-expression