ノードの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