select t.*,level from table t
start with b='0000'
connect by prior b=a

解决方案 »

  1.   

    这样写更清晰一点吧.
    select t.b,level from table t
    start with b='0000'
    connect by prior b=a
    order by level,b
      

  2.   

    select 本级部门
    from 表
    connect by prior 本级部门=上级部门
    start with 上级部门='0000';
      

  3.   

    SQL> select * from t;ID   PID
    ---- ----
    0000 
    0001 0000
    0002 0000
    0003 0000
    0011 0001
    0012 0001
    0021 0002
    0022 0002
    0031 0003
    0032 0003
    0033 000311 rows selectedSQL> select id from t start with id = '0000' connect by pid = prior id;ID
    ----
    0000
    0001
    0011
    0012
    0002
    0021
    0022
    0003
    0031
    0032
    003311 rows selectedSQL> select id,level from t start with id = '0000' connect by pid = prior id;ID        LEVEL
    ---- ----------
    0000          1
    0001          2
    0011          3
    0012          3
    0002          2
    0021          3
    0022          3
    0003          2
    0031          3
    0032          3
    0033          311 rows selected