日期:2014-05-18  浏览次数:20486 次

急急.....这个树形结构的SQL要怎样写啊!
现有两张表   a   表结构如下
id tree_code               bill_code             bill_name bill_unit
1 10000 100 总则
2 20000 200 路
3 30000 300
4 1000010000 101
5 100001000010000                     101-1
6 10000100001000010000 101-1-a
7 10000100001000010001 101-1-b
8 2000010000 201
9 200001000010000 201-1
10               200001000010001                           202-1
10 20000100001000010000 201-1-a
其中tree_code为树形结构编码每5位为一个层次.最顶层的tree_code为5位数.他的父节点就是该tree_code长度-5,
b表结构如下
id           tree_code                 bill_code             quality
1 10000100001000010000 101-1-a 10     2 20000100001000010000 201-1-a 30
该表的tree_code为   A表的tree_code   且存的是叶子点.现要得到一个如下的结果集
tree_code           bill_code               bill_name
10000 100 总则
1000010000 101
100001000010000                     101-1
10000100001000010000 101-1-a
20000 200 路
200001000010000 201
200001000010000 201-1
20000100001000010000 201-1-a    
也就是从叶子节点向上一直找到顶层节点.大侠们帮帮手吧.这个SQL要怎么写啊.想了一整天了!!!!

------解决方案--------------------
10000 100 总则
1000010000 101
100001000010000 101-1
10000100001000010000 101-1-a
20000 200 路
2000010000 201
200001000010000 201-1
20000100001000010000 201-1-a