请教高手,递归查询五级节点的所有数据。在线等待,谢谢id          name           parent_dept_id1           全国           root
2           中国           1
3           广东省         2
4           深圳市         3
5           龙岗区         46           坂田街道       5
7           养美村         6
8           广州市         3
9           番寓区         810          市桥           9
11          美国           1
12          纽约省         11
13          纽约市         12
14          纽约区         1315          纽约街道       14怎样取出区的数据,如下数据是所要的数据。5           龙岗区         4
9           番寓区         8
14          纽约区         13

解决方案 »

  1.   

    id          name           parent_dept_id1           全国           root
    2           中国           1
    3           广东省         2
    4           深圳市         3
    5           龙岗区         4
    6           坂田街道       5
    7           养美村         6
    8           广州市         3
    9           番寓区         8
    10          市桥           9
    11          美国           1
    12          纽约省         11
    13          纽约市         12
    14          纽约区         13
    15          纽约街道       14
    刚数据有点乱。。
      

  2.   


    with tab as(
    select '1' id, '全国' name, 'root' parent_dept_id from dual union all
    select '2', '中国', '1' from dual union all 
    select '3', '广东省', '2' from dual union all
    select '4', '深圳市', '3' from dual union all
    select '5', '龙岗区', '4' from dual union all
    select '6', '坂田街道', '5' from dual union all
    select '7', '养美村', '6' from dual union all
    select '8', '广州市', '3' from dual union all
    select '9', '番寓区', '8' from dual union all
    select '10', '市桥', '9' from dual union all
    select '11', '美国', '1' from dual union all
    select '12', '纽约省', '11' from dual union all
    select '13', '纽约市', '12' from dual union all
    select '14', '纽约区', '13' from dual union all
    select '15', '纽约街道', '14' from dual
    )
    select id,name,parent_dept_id from tab
    where level=5   --第5层节点,加个条件就行了啊
    start with parent_dept_id='root'
    connect by prior id = parent_dept_id
    ID NAME   PARENT_DEPT_ID
    ---------------------------
    5  龙岗区 4 
    9  番寓区 8 
    14 纽约区 13
      

  3.   

    又见树形题,
    start with
    connect by
      

  4.   

    受教了,level列是哪里来的??