当前位置: 代码迷 >> DB2 >> db2递归有关问题请问
  详细解决方案

db2递归有关问题请问

热度:6774   发布时间:2013-02-26 00:00:00.0
db2递归问题请教
关于在db2中的递归问题
比如我有个表slj_test_005 col_1 –代码字段,col_2 –表示col_1的父级,col_3 –表示col_1所属级别
如果表中有数据
COL_1 COL_2 COL_3
1001 1
100101 1001 2
100102 1001 2
10010101 100101 3
10010201 100102 3
1002 1
100201 1002 2
100202 1002 2
如果:
with tmptab(col_3,col_1)as
 (
  select col_3,col_1 from slj_test_005 --where col_3 = 3
  union all
  select b.col_3,b.col_1 
  from tmptab a, slj_test_005 b
  where a.col_1 = char(b.col_3)
 )select * from tmptab;
则出来的结果:
COL_3 COL_1
1 1001
2 100101
2 100102
3 10010101
3 10010102
3 10010201
3 10010202
1 1002
2 100201
2 100202
如果我想得到的结果是
COL_3 COL_1
1 1001
2 100101
3 10010101
3 10010102
2 100102
3 10010201
3 10010202
1 1002
2 100201
2 100202
该怎么写啊?

------解决方案--------------------------------------------------------
如果对结果的col_1排序其结果如何呢?
------解决方案--------------------------------------------------------
select * from tmptab order by col_1
  相关解决方案