一个表tb1,有如下字段:
出发地     目的地
a          b
a          c
a          d
b          c
c          e
d          f
e          g
g          a
f          h
问从出发地a可以到达的所有目的地,包括间接到达的,比如包括目的地e,用sql或程式实现均可,请高手赐教

解决方案 »

  1.   

    一个树形结构的sql
    connect by
      

  2.   

    SQL> select c1,c2 from pp_test;
     
    C1                                                                             C2
    -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
    a                                                                               d
    a                                                                               c
    a                                                                               b
    b                                                                               c
    c                                                                               e
    d                                                                               f
    e                                                                               g
    f                                                                                h
     
    8 rows selected
     
    SQL> 
    SQL> select sys_connect_by_path(c1, '-')
      2    from pp_test
      3   start with c1 = 'a' connect by prior c2 = c1;
     
    SYS_CONNECT_BY_PATH(C1,'-')
    --------------------------------------------------------------------------------
    -a
    -a
    -a-c
    -a-c-e
    -a
    -a-b
    -a-b-c
    -a-b-c-e
     
    8 rows selected
     
    SQL> 
      

  3.   

     关键是start with 出发地 = 'a' connect by prior 目的地 = 出发点;
      

  4.   

    得到的并不是我想要的结果,我所要的结果是从a出发可到达的所有目的地,所以可以到达的目的地有:a,b,c,d,e,f,g,h 可能各位误解我的意思了,a 间接可以到达的目的地也要包括,如e,f,g,h
      

  5.   

    你这个数据里有一个循环调用,即a->c->e->g->a,这样好象不行吧?
    5楼的答案很好啊,如果在前面再加一个distinct就完美了,呵呵:)
    select distinct sys_connect_by_path(c1, '-')
    from pp_test
    start with c1 = 'a' connect by prior c2 = c1;
      

  6.   

    五楼的答案并不符合题目的要求啊,各位好像并没有测试或者看清题意吧,我要的是从出发地a,出发可以到达的所有的目的地,包括间接的,答案应该是a,b,c,d,e,f,g,h,以上目的地从a出发均可到达,所以五楼的想法虽然好,但是并不符合题目的要求
      

  7.   


    create or replace function ffindmd(c_cf varchar2)
    return varchar2
    is
       type tb1_cursor_type is ref cursor;
       tb1_cursor tb1_cursor_type;
       amount int:=0;
       v_cf tb1.cf%type;
       res varchar2(500);
       v_temp tb1.cf%type;
    begin
       select count(*) into amount from tb1 where cf=c_cf and md<>'a';
       if amount=0 then
          return '';
       else
          open tb1_cursor for select md from tb1 where cf=c_cf and md<>'a';
          dbms_output.put_line('出发点:'||c_cf);
          loop
             fetch tb1_cursor into v_cf;
             exit when tb1_cursor%notfound;
             dbms_output.put_line(c_cf||'的目的:'||v_cf);
             --res:=res||' , '||v_cf;
             v_temp:=ffindmd(v_cf);
             res:=res||v_temp||v_cf;
             --dbms_output.put_line(res);
          end loop;
          close tb1_cursor;
          --dbms_output.put_line(res);
          return res;
       end if;
    end ffindmd;
    select ffindmd('a') from dual ;结果我没有去掉重复的..重复的用字符串处理下就是了
      

  8.   


    SQL> select distinct 'a' || sys_connect_by_path(p2, '-') from PP_TEXT start with p1 = 'a' connect by
     prior p2 = p1;'A'||SYS_CONNECT_BY_PATH(P2,'-')
    --------------------------------------------------------------------------------
    a-b
    a-b-c
    a-b-c-e
    a-b-c-e-g
    a-c
    a-c-e
    a-c-e-g
    a-d
    a-d-f
    a-d-f-h10行が選択されました。在5楼的基础上做的
      

  9.   

    select
    distinct substr(path,length(path),1)
    from
    (
    select distinct sys_connect_by_path(trim(c1), '-') path from pp_test start with c1 = 'a' connect by prior c2 = c1
    );
      

  10.   

    如果用的是10G
    需要先
    delete from pp_test
    where c2 = 'a';
      

  11.   

    select distinct t.b
    from (select a,b from testroad where b!='a') t 
    start with t.a = 'a'  connect by prior t.b = t.a