我想设计一棵树,在treeview中显示。一个好象windows资源管理器的东西。
在最后一个节点有其自己的属性,就好象文件夹里的文件那样。
对最后那个节点是可以进行查询统计的。
比如:
A
a1
a1-1(a1-1为最终节点)
a1-2
a1-2-1(a1-2-1为最终节点)
a2(a2为最终节点)
B
b1
b1-1
b1-1-1(b1-1-1为最终节点)
C(C为最终节点)注意:以上每个不是最终节点的点都可以有n级节点后再有最终节点,但最后那个节点肯定都是最终节点。我想做到可以对每一个最终节点做统计。
比如说我我上面的最终节点的属性都有“年龄”这个属性,我要查询跟统计25岁的有多少人。
请问个位大侠,我要怎样设计这个数据库呢??
在最后一个节点有其自己的属性,就好象文件夹里的文件那样。
对最后那个节点是可以进行查询统计的。
比如:
A
a1
a1-1(a1-1为最终节点)
a1-2
a1-2-1(a1-2-1为最终节点)
a2(a2为最终节点)
B
b1
b1-1
b1-1-1(b1-1-1为最终节点)
C(C为最终节点)注意:以上每个不是最终节点的点都可以有n级节点后再有最终节点,但最后那个节点肯定都是最终节点。我想做到可以对每一个最终节点做统计。
比如说我我上面的最终节点的属性都有“年龄”这个属性,我要查询跟统计25岁的有多少人。
请问个位大侠,我要怎样设计这个数据库呢??
转化为关系模型就有很多冗余。
用LDAP比用传统数据库有效多了。
Oracle也支持LDAP架构,你看文档好了。
然后把这个树各个接点的关系都输入这个表中。这样一个表可以保存很多棵树。
制造业中的产品bom表就是这样的树,可以存在这样结构的表中。要形成或者查询某棵树的结果时只要输入一个最上层接点,然后可以查出这个接点的所有子接点。
这个查询可以写一个第归函数来实现。
这个第归函数如下:从单层BOM表中检索产品结构:
单层bom表结构:
fatherid | childid | childnum
----------------------------------------
中 日 1
中 法 2
中 英 3
俄 加 5
俄 西 9
英 俄 2
英 美 7检索函数:CREATE function dbo.f_id(@pcode varchar(15))
returns @re table(childid varchar(15),[level] int,sid varchar(8000),ifyeah varchar(5),num int)
as
begin
declare @l int
set @l=1
insert @re select distinct fatherid,@l,fatherid,'0' as ifyeah,num=1
from singlebom a
where not exists(select * from singlebom where childid=a.fatherid) and fatherid=@pcode
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.childid,@l, ltrim(rTrim(b.sid))+ '>'+a.childid,'1' as ifyeah ,num=a.childnum*b.num
from singlebom a,@re b
where a.fatherid=b.childid and b.[level]=@l-1 and a.childid not in (select fatherid as childid from singlebom )
insert @re select a.childid,@l, ltrim(rTrim(b.sid))+ '>'+a.childid,'0' as ifyeah ,num=a.childnum*b.num
from singlebom a,@re b
where a.fatherid=b.childid and b.[level]=@l-1 and a.childid in (select fatherid as childid from singlebom ) end
return
end调用检索函数:SELECT [曾數] = LEVEL, [貨號] = childid, [是否葉子] = ifyeah, [數量] = num,
[排序] = sid
FROM f_id('中')
ORDER BY sid查询结果:
层数货号是否叶子数量结构
1中 01中
2日 11中 >日
2法 12中 >法
2英 03中 >英
3俄 06中 >英 >俄
4加 130中 >英 >俄 >加
4西 154中 >英 >俄 >西
3美 121中 >英 >美