如上两个表,部门可以有子部门,部门里面有部门人员,现如何通过Oracle的树遍历查询命令
 select
connect by {prior col1 = col2 || col1 = prior col2 }
 [start with ]
得到一棵这样的树

解决方案 »

  1.   

    看不清你图中的表结构。给你一个oracle中emp查询上下级关系的例子。
    select ename,LEVEL from emp start with ename='KING'  connect by prior EMPNO=MGR
    /
      

  2.   

    就是两张表了,大家帮一下忙,如果值查询部门信息表,获得树的sql语句很好写,已经写出来了,但两个表查询得到一棵树就不好弄了。
      

  3.   

    这么简单的问题connect by + join呗,图看不清楚,你可以把建表语句写出来嘛,搞个图,连个测试数据都没有,难道让别人自己建表?
      

  4.   

    ok,类似这样的,自己要什么仿造着搞呗
    SQL> select * from s_dept_1;
     
       DEPT_ID DEPT_NAME PARENT_DEPT_ID
    ---------- ---------- --------------
             1 部门     
             2 部门1                1
             3 部门11               2
             4 部门12               2
             5 部门2                1
             6 部门21               5
     
    6 rows selected
     
    SQL> select * from s_dept_staff;
     
       DEPT_ID   STAFF_ID STAFF_NAME
    ---------- ---------- ----------
             3          1 垃圾1
             3          2 垃圾2
             3          3 垃圾3
             4          4 垃圾4
             4          5 垃圾5
             6          6 垃圾6
             6          7 垃圾7
     
    7 rows selected
     
    SQL> 
    SQL> select lpad(dept_name,lengthb(dept_name)+(level*2)-2,'    ')
      2   from(
      3  select  dept_id,dept_name,parent_dept_id
      4  from s_dept_1
      5  union all
      6  select null,staff_name,dept_id
      7  from s_dept_staff)
      8  start with parent_dept_id is null
      9  connect by prior dept_id = parent_dept_id;
     
    LPAD(DEPT_NAME,LENGTHB(DEPT_NA
    --------------------------------------------------------------------------------
      部门
        部门1
          部门11
            垃圾1
            垃圾2
            垃圾3
          部门12
            垃圾4
            垃圾5
        部门2
          部门21
            垃圾6
            垃圾7
     
      

  5.   

    前台做也很简单,加level,什么全部展开,全部关闭,点上级显示下级,都很简单,弄个div,添加点event,就ok了
    一般都是有专门的js