日期:2014-05-17  浏览次数:20971 次

需求一个SQL语句
现有表t_belong
id father_id child_id

1 1002 1003
2 1002 1004
3 1003 1005
4 1004 1006
5 1004 1007

如果通过 father_id=1002 获取该记录下所有的child_id记录即:1003,1004,1005,1006,1007;
类似一个树形结构;如果根据根节点获取下面所有的子节点





------解决方案--------------------
SQL code
select wm_concat( child_ID ) from t_belong t   
  start with t.father_id=1002   
  connect by prior t.child_id=t.father_id

------解决方案--------------------
SQL code

create table t_belong(
id number(18,0),
father_id number(18,0),
child_id number(18,0)
);

insert into t_belong(id,father_id,child_id) values(1, 1002, 1003);
insert into t_belong(id,father_id,child_id) values(2, 1002, 1004);
insert into t_belong(id,father_id,child_id) values(3, 1003, 1005);
insert into t_belong(id,father_id,child_id) values(4, 1004, 1006);
insert into t_belong(id,father_id,child_id) values(5, 1004, 1007);