现有一个部门表(department)
id  部门ID
mc  部门名称
rid 上级部门id
用户表(user)
  userid   用户id
  username 用户名称
  userdep  用户所有机构id
现在要查询到部门id=2 的所有其下机构的用户,如何查询?谢谢

解决方案 »

  1.   

    ---用这个思路实验下吧
    select t. userid   from (select a.id,a.rid,b.userid from department a,user b where a.id=b.userdep) t
         connect by prior id = rid 
         start with id=2;
      

  2.   

    部门表(department)与用户表(user)是通过userdep与id关联的吧select t1.userid, t1.username, t1.userdep
      from user t1, department t2
     where t1.userdep = t2.id
       and t2.id = '2';
      

  3.   

    select userid, username
    from user
    where userdep in  
      (select id from department start with id=2
     connect by rid= prior id)
      

  4.   

    用树结构查询
    就是start with ..... connect by