首页 > 数据库技术 > 详细

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

时间:2019-06-25 11:05:23      阅读:115      评论:0      收藏:0      [点我收藏+]

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=‘济南‘

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!