有两张表如下
表1是原始表,表2的内容是把上级和下级的全部对应起来,包括自己,
比如表1上级2对应的下级有30,20,然后30的下级又有300
在表2中就要写4条记录,分别为30,20,300,2这几个下级。不知道大伙能听明白否
表1
上级    下级
1      20
2      30
3      10
2      20
30 300表2
上级    下级
1 20
1 1
2 30
2 20
2 2
3 10
3 3
30 300
2 300
300 300

解决方案 »

  1.   

    select * from  test
    start with 上级=1  //上级
    connect by prior 下级=上级prior 表示上条纪录
    prior 下级=上级   上条纪录的 下级=本条的 上级具体找start with用法
      

  2.   

    我确实写错了!
    如果只有
        2--30--300 
        3层的话可以用union来写
    select a.上级,b.下级 from (select * from  test
    start with 上级 =2
    connect by prior 下级=上级) a,test b
    where a.下级=b.上级
    union 
    select * from test
    union select * from test
    union select 上级,上级 as 下级 from test希望有更好的答案
      

  3.   

    测试数据
    create table test(
    上级  varchar(20),
    下级  varchar(20))insert into test
    select '1','20' from dual
    union
    select '2','30' from dual
    union
    select '3','10' from dual
    union
    select '2','20' from dual
    union
    select '30','300' from dual测试结果
        上级 下级
    1 1 1
    2 1 20
    3 2 2
    4 2 20
    5 2 30
    6 2 300
    7 3 10
    8 3 3
    9 30 30
    10 30 300
      

  4.   

    HelloWorld_001(Hello)
    我不是很看的明白你的回帖
      

  5.   

    我只能用其他方式来解决,请参考。如果不符合要求,算我白写.
    -----------------------------------------------------------------------SQL> create table a(u number(3),l number(3))2  ;Table createdSQL> insert into a(u,l) values(1,20);1 row inserted
    SQL> insert into a(u,l) values(2,30);1 row insertedSQL> insert into a(u,l) values(3,10);1 row insertedSQL> insert into a(u,l) values(2,20);1 row insertedSQL> insert into a(u,l) values(30,300);1 row inserted
    SQL> commit;Commit complete1,创建类型create or replace type r_type is object(u number,l number);
    create or replace type t_table is table  of r_type;2,创建function
    create or replace  function getData return t_table
        as
         cursor ca is select distinct u from a;
         rs ca%rowtype;
         cursor cb(kk number) is select * from a connect by prior l=u start with u=kk; 
         rr cb%rowtype;   
         rs2 t_table:=t_table();     
         liu number;
         i integer;
         begin
             i:=1;
             open ca;
             loop
                fetch ca into rs;
                exit when ca%notfound;
                open cb(rs.u);
                loop
                   fetch cb into rr;
                   exit when cb%notfound;
                      rs2.extend(1);
                      rs2(i):= r_type(rs.u,rr.l);
                      i:=i+1;
                end loop;
                rs2.extend(1);
                rs2(i):= r_type(rs.u,rs.u);
                i:=i+1;
                close cb;
             end loop;
             close ca;
             return rs2;
       end;3,查询验证
    SQL> select * from table(getdata);         U          L
    ---------- ----------
            30        300
            30         30
             1         20
             1          1
             2         30
             2        300
             2         20
             2          2
             3         10
             3          310 rows selected
      

  6.   

    TO precipitant(塞北的雪) 
      能否加我QQ,还有问题请教一下2770967