请教一下!我有两张表,table1和table2
table1表中有id,name,parentID三个字段,其中parentID纪录的是父级id,就是一个树形结构,table2表中有table1中的id和userID
我的问题是,如何能用sql找出,某条userID满足条件的数据的table1的数据及其所有上级、上级的上级的节点的id
谢谢!

解决方案 »

  1.   


    with table1 as
    (
     select 1 id,'root' name,0 parentID from dual
     union 
     select 2,'col1',1 from dual
     union
     select 3,'col2',2 from dual
     union
     select 4,'col3',3 from dual
     ),
     table2 as
     (
     select 4 id, 1 userid from dual
     )
     select * from table1 
        start with id = (select id from table2 where userid = 1)
          connect by   id =prior   parentid
      

  2.   

    数据中的层次关系如下图:
    实测结果:
    manager_id列,显示的就是7号员工所有领导的员工编号。一直到根结点。
      

  3.   

    sys_connect_by_path(字段,'分隔符')