挑战性问题,如何生成带线的树形结构结果集??

zzy9903 2007-01-31 11:10:21
我的库表:
Id Name     ParentID
0 中国 0
1 山东 0
2 广东 0
3 济南 1
4 青岛 1
5 广州 2
6 佛山 2
7 东莞 2
8 虎门 2
9 历下区 3
10 白云区 5

如何根据库表检索生成如下结构的结果集?

□┬中国
├┬山东
│├┬济南
││└─历下区
│└─青岛
└┬广东
├┬广州
│└─白云区
├─佛山
├─东莞
└─虎门

这个问题好像有点复杂哦,不是单纯的行缩进那么简单,还要加上双字节的制表符号组成树结构连线,偶想了半天不知该如何下手,只好偷个懒到这来请教一下有没有那位高人做过,给指点一下,谢谢。
...全文
571 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzy9903 2007-05-10
  • 打赏
  • 举报
回复
非常感谢hinco(桃色德鲁依)
Hinco 2007-03-19
  • 打赏
  • 举报
回复
第一层parentID是null也行(相应第一次插入的条件要写成parentid is null),只要不是id里有的就可以
Hinco 2007-03-19
  • 打赏
  • 举报
回复
我上面的答案还要做点小修改,防止位置错乱不同位数字符串比较的错乱
create table tree(id int,Name varchar(10),ParentID int)
insert tree select 0, '中国' ,-1
union all select 1, '山东' ,0
union all select 2, '广东' ,0
union all select 3, '济南' ,1
union all select 4, '青岛' ,1
union all select 5, '广州' ,2
union all select 6, '佛山' ,2
union all select 7, '东莞' ,2
union all select 8, '虎门' ,2
union all select 9, '历下区' ,3
union all select 10, '白云区' ,5
union all select 21, '测试区' ,1
create table #temp(id int,Name varchar(100),ParentID int,
treecode varchar(1000),level int)
declare @level int
select @level=0
insert into #temp select id,'□'+
case when exists(select 1 from tree where id<>a.id and parentid=a.id)
then '┬' else '─' end +name,parentid,'0',0
from tree a where parentid=-1
while @@rowcount>0
begin
set @level=@level+1
insert into #temp select a.id,' '
+replace(replace(substring(b.name,2,@level-1),'└',' '),'├','│')
+case when not exists(select 1 from tree where parentid=b.id and
convert(varchar,id)>convert(varchar,a.id)) then '└' else '├' end
+case when exists(select 1 from tree where id<>a.id and parentid=a.id)
then '┬' else '─' end
+a.name,
a.parentid,b.treecode+'.'+convert(varchar,a.id),@level
from tree a inner join #temp b on
a.parentid=b.id and b.level=@level-1
end
select * from #temp order by treecode,id

drop table #temp
drop table tree

name
------------------------------
□┬中国
 ├┬山东
 │├─测试区
 │├┬济南
 ││└─历下区
 │└─青岛
 └┬广东
  ├┬广州
  │└─白云区
  ├─佛山
  ├─东莞
  └─虎门
Hinco 2007-03-19
  • 打赏
  • 举报
回复
楼主给的数据不好,主要是第一条中国的parentID不应该是自己,应该改成一个负数,如-1,我下面的解法就是把第一层parentid默认为-1的写法(要注意''之内的空格要使用全角空格才好对奇)
create table tree(id int,Name varchar(10),ParentID int)
insert tree select 0, '中国' ,-1
union all select 1, '山东' ,0
union all select 2, '广东' ,0
union all select 3, '济南' ,1
union all select 4, '青岛' ,1
union all select 5, '广州' ,2
union all select 6, '佛山' ,2
union all select 7, '东莞' ,2
union all select 8, '虎门' ,2
union all select 9, '历下区' ,3
union all select 10, '白云区' ,5
create table #temp(id int,Name varchar(100),ParentID int,
treecode varchar(1000),level int)
declare @level int
select @level=0
insert into #temp select id,'□'+
case when exists(select 1 from tree where id<>a.id and parentid=a.id)
then '┬' else '─' end +name,parentid,'',0
from tree a where parentid=-1
while @@rowcount>0
begin
set @level=@level+1
insert into #temp select a.id,' '
+replace(replace(substring(b.name,2,@level-1),'└',' '),'├','│')
+case when not exists(select 1 from tree where parentid=b.id and id>a.id)
then '└' else '├' end
+case when exists(select 1 from tree where id<>a.id and parentid=a.id)
then '┬' else '─' end
+a.name,
a.parentid,b.treecode+'.'+convert(varchar,a.id),@level
from tree a inner join #temp b on
a.parentid=b.id and b.level=@level-1
end
select Name from #temp order by treecode,id

drop table #temp
drop table tree

--结果
Name
------------------------------
□┬中国
 ├┬山东
 │├┬济南
 ││└─历下区
 │└─青岛
 └┬广东
  ├┬广州
  │└─白云区
  ├─佛山
  ├─东莞
  └─虎门
heilong05 2007-03-19
  • 打赏
  • 举报
回复
Mark
zzy9903 2007-02-02
  • 打赏
  • 举报
回复
可以加一个深度字段,那不是问题,能告诉我你是怎么作的吗?
ldw701 2007-02-02
  • 打赏
  • 举报
回复
这几个字段好像不够,以前做过一个类似的,用在dropdownlist中,最起码还要一个深度字段。。,才能构建出来
zzy9903 2007-02-01
  • 打赏
  • 举报
回复
谢谢,梅花雪的树确实用在我的网站里,但是这个问题不是作网站用的,是干别的用的,不能在客户端实现。
txt_ly 2007-02-01
  • 打赏
  • 举报
回复
如果仅仅是要结果的话.参考一下*---------------------------------------------------------------------------*\
| Subject: Web TreeView Class |
| Version: 1.0 |
| Author: 黄方荣【meizz】【梅花雪】 |
| FileName: MzTreeView.js |
| Created: 2004-10-18 |
| LastModified: 2005-03-10 |
| Download: http://www.meizz.com/Web/Download/MzTreeView10.rar |
| Explain: http://www.meizz.com/Web/Article.asp?id=436 |
| Demo: http://www.meizz.com/Web/Demo/MzTreeView10.htm |
| |
| You may use this code on your item |
| this entire copyright notice appears unchanged |
| and you clearly display a link to http://www.meizz.com/
mlb2729 2007-02-01
  • 打赏
  • 举报
回复
路過,支持下.................
xiaoku 2007-02-01
  • 打赏
  • 举报
回复
oh,要求确实很猛!
zzy9903 2007-02-01
  • 打赏
  • 举报
回复
看了,周老大的结果不是很理想,求更好的。
marco08 2007-01-31
  • 打赏
  • 举报
回复
--参考,邹老大写的方法

--测试数据
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001',NULL ,'山东省'
UNION ALL SELECT '002','001','烟台市'
UNION ALL SELECT '004','002','招远市'
UNION ALL SELECT '003','001','青岛市'
UNION ALL SELECT '005',NULL ,'四会市'
UNION ALL SELECT '006','005','清远市'
UNION ALL SELECT '007','006','小分市'

--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
FROM @t a,@t_Level b
WHERE a.PID=b.ID
AND b.Level=@Level-1
END

--显示结果
SELECT SPACE(b.Level*2)+'|--'+a.Name
FROM @t a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort
/*--结果
|--山东省
|--烟台市
|--招远市
|--青岛市
|--四会市
|--清远市
|--小分市
--*/
cxmcxm 2007-01-31
  • 打赏
  • 举报
回复
好象无直接的sql语句可生成,用存储过程慢慢编吧.
zzy9903 2007-01-31
  • 打赏
  • 举报
回复
□┬中国
 ├┬山东
 │├┬济南
 ││└─历下区
 │└─青岛
 └┬广东
  ├┬广州
  │└─白云区
  ├─佛山
  ├─东莞
  └─虎门

csdn的编辑器挺烦人的,排好的格式被它把空格删掉了

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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