首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

急这个树形结构的SQL要怎样写啊解决方案

2012-01-28 
急急.....这个树形结构的SQL要怎样写啊!现有两张表a表结构如下idtree_codebill_codebill_namebill_unit110

急急.....这个树形结构的SQL要怎样写啊!
现有两张表   a   表结构如下
idtree_code               bill_code             bill_namebill_unit
110000100总则
220000200路
330000300
41000010000101
5100001000010000                     101-1
610000100001000010000101-1-a
710000100001000010001101-1-b
82000010000201
9200001000010000201-1
10               200001000010001                           202-1
1020000100001000010000201-1-a
其中tree_code为树形结构编码每5位为一个层次.最顶层的tree_code为5位数.他的父节点就是该tree_code长度-5,
b表结构如下
id           tree_code                 bill_code             quality
110000100001000010000101-1-a10     220000100001000010000201-1-a30
该表的tree_code为   A表的tree_code   且存的是叶子点.现要得到一个如下的结果集
tree_code           bill_code               bill_name
10000100总则
1000010000101
100001000010000                     101-1
10000100001000010000101-1-a
20000200路
200001000010000201
200001000010000201-1
20000100001000010000201-1-a    
也就是从叶子节点向上一直找到顶层节点.大侠们帮帮手吧.这个SQL要怎么写啊.想了一整天了!!!!

[解决办法]
10000100总则
1000010000101
100001000010000101-1
10000100001000010000101-1-a
20000200路
2000010000201
200001000010000201-1
20000100001000010000201-1-a

热点排行