在一个邻接表中,给定一个节点的id,我该如何找到它对应的根节点?
注:
该表包含多个树,因此我不能简单地搜索null parentId。
更多信息:
这是我目前拥有的,是否存在任何问题或改进?
注:
该表包含多个树,因此我不能简单地搜索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