我有一个表,内记录的树型结构的信息。
即:id/parent_id/name,现要将其按照树型结构展现出来。
我现在用的是普通的深度优先法遍历整个树,效率很底,
可以前我见过一个高手在oracle里用一个SQL就可实现这种查询,请问这个SQL应该怎么写?
先谢了。

解决方案 »

  1.   

    普通的深度优先法?是什么方法?
    是connect by + start with吗?
      

  2.   

    http://expert.csdn.net/Expert/topic/1551/1551178.xml?temp=.7578394
      

  3.   

    SELECT NAME FROM table1
    START WITH ID = :ID 
    CONNECT BY PRIOR SUPERID = ID;
      

  4.   

    create table testtree(id int,priorid int,name varchar2(20));
    insert into testtree values(1,0,'中国a');
    insert into testtree values(2,0,'美国');
    insert into testtree values(3,0,'加拿大');
    insert into testtree values(4,1,'北京');
    insert into testtree values(5,1,'上海');
    insert into testtree values(6,1,'江苏');
    insert into testtree values(7,6,'苏州');
    insert into testtree values(8,7,'常熟');
    insert into testtree values(9,6,'南京');
    insert into testtree values(10,6,'无锡');
    insert into testtree values(11,2,'纽约');
    insert into testtree values(12,2,'旧金山');
    ----全显示-----
    select lpad(' ',level-1,' ')||name name from testtree a start with priorid=0 connect by prior id = priorid;
      

  5.   

    ----按子找父---
    select lpad(' ',level-1,' ')||name name from testtree a start with id=8 connect by  id=prior priorid;---按父找子--
    select lpad(' ',level-1,' ')||name name from testtree a start with id=1 connect by prior id=priorid;
      

  6.   

    给你一个例子看看:
    SELECT DISTINCT A.USERID,B.DWBH00,C.DWMC00 FROM SYSUSER_USERLIST A,SYSUSER_ZGYQXB  B ,DA_DWXX00 C WHERE A.USERID=B.USERID AND B.DWBH00=C.DWBH00  AND A.FZXBH0=C.FZXBH0 AND B.USERID IN (SELECT USERID FROM SYSUSER_USERLIST CONNECT BY USERID=PRIOR USERGR START WITH USERID=:TMP) ORDER BY  USERID表SYSUSER_USERLIST 是一树型表,USERID为主关键字