sql写法,子节点名称拼接上级节点名称

时间:2019-06-25 11:05:23   收藏:0   阅读:115

with T(id,[name],pid) as
(select 1,N‘中国‘,-1 union all
select 2,N‘山东‘,1 union all
select 3,N‘济南‘,2 union all
select 4,N‘青岛‘,2 union all
select 5,N‘历下区‘,3 union all
select 6,N‘崂山区‘,4 union all
select 7,N‘河北‘,1 union all
select 8,N‘石家庄‘,7),
cte as
(select id,convert(varchar,name) as name,pid,[name] as nam from T where pid=-1
       union all
  select t.id,convert(varchar,a.name+‘-‘+t.name),t.pid,t.name  from cte a,t where a.id=t.pid)
  
 select * from cte where nam=‘济南‘

原文:https://www.cnblogs.com/zhangchengye/p/11081303.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!