【发布时间】:2012-07-25 09:13:30
【问题描述】:
在邻接列表表中,给定一个节点的id,我如何找到它的关联根节点?
注意:
该表包含多棵树,因此我不能简单地搜索 null parentId。
更多信息:
这是我目前遇到的问题,对此有何问题或改进?
with tree as
(
select
t.*
from table1 t
where t.id = @id
union all
select
t2.*
from tree
join table1 t2 on tree.parentId = t2.id
)
select *
from tree
where parentId is null
【问题讨论】:
标签: sql sql-server tree root adjacency-list