求解答。

looklooksee 2012-07-11 12:07:12
各位老大。。想问下。。。

名称 名号 级别
国家 01 1
省 0101 2
市 010101 3
地区 01010101 4

这样的数据有好多,怎么让他自动匹配成这样的数据?
例子------------------->结果
地区 国家>省>市>地区
省 国家>省
市 国家>省>市
用SQL能实现么?
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
looklooksee 2012-07-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
给个案例 参考,套用一下下

SQL code

if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,name varchar(1),pid int)
insert into [tb]
select 1,'A',0 union all
select 2,'B',1 union all
……
[/Quote]


吃完饭 去试下。。。谢谢 吼
looklooksee 2012-07-11
  • 打赏
  • 举报
回复
吃完饭 去试下。。。谢谢 吼
Felixzhaowenzhong 2012-07-11
  • 打赏
  • 举报
回复
给个案例 参考,套用一下下
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,name varchar(1),pid int)
insert into [tb]
select 1,'A',0 union all
select 2,'B',1 union all
select 3,'D',1 union all
select 4,'C',2 union all
select 5,'D',2 union all
select 6,'A',4 union all
select 7,'E',5 union all
select 8,'F',5
GO
;with cte
as
(
select *,[path]=cast([name]+'->' as varchar(100)) ,[level] = 1 from tb where pid = 0
union all
select a.*, cast(c.[path]+a.[name]+'->' as varchar(100)),[level]+1 from cte c ,tb a where a.pid = c.id
)
select
*
from cte
老白猫 2012-07-11
  • 打赏
  • 举报
回复
select 国家+‘>’+省+‘>’+市+‘>’+地区 as 地区 from
SQL77 2012-07-11
  • 打赏
  • 举报
回复
BOM。。。精华

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧