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

解决方案 »

  1.   

    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);
      

  2.   

    我自己回答吧 哎select   t.*   from  t_belong t    
      start   with   t.father_id=1002   
      connect   by   prior   t.child_id=t.father_id 
      

  3.   

    你是要一个等级表是吧,你这样与用where father_id=1002有区别么
      

  4.   

    select wm_concat( child_ID ) from t_belong t   
      start with t.father_id=1002   
      connect by prior t.child_id=t.father_id 
      

  5.   


    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);SELECT lpad('|-', (level - 1) * 4, ' ') || lpad('『', 2) || id ||
           rpad('』', 2) id
      FROM t_belong
    CONNECT BY PRIOR child_id = father_id
     START WITH id = 2;
    ..