SQL根据指定节点ID获取所有父级节点和子级节点
文章目录
根据指定节点ID获取所有父节点
with temp as(
select * from dbo.Category where Id=493 --表的主键ID
union all
select t.* from temp,dbo.Category t where temp.Pid=t.Id --父级ID=子级ID
)select * from temp order by Level;
根据指定节点ID获取所有子节点
with temp as(
select * from dbo.Category where Id=344 --表的主键ID
union all
select t.* from temp,dbo.Category t where temp.Id=t.Pid --子级ID==父级ID
)select * from temp;
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论