现有三个表 
表一 
lxid  name 
01    红文具盒 
02    铅笔 
03    圆珠笔
04    蓝文具盒
表二
deptid   name
1        书包一
2        书包二
3        书包三
表三 
id    lxid  upid  deptid
1    01             1
2    02  01         1
3    03  01         1
......现在想得到如下结果 
书包一
          红文具盒
                     铅笔
                     圆珠笔
书包二  
          蓝文具盒
                     铅笔
...... 

解决方案 »

  1.   

    start with ...
    connect by ....语句
      

  2.   

    select lpad(' ',level*6)||表2.name
      from 表1,表2,表3
     where 表2.deptid = 表3.deptid
       and 表1.bid = 表3.bid
    START WITH 表3.bid = '01'
    CONNECT BY PRIOR 表3.bid = 表3.upid
      

  3.   


    用start with... connect by ...分层语句。
      

  4.   

    用start with ... connect by ... 然后再在前面加前导空格
      

  5.   

    select * from 
           (select name t1, deptid t2, 0 t3 from t02 
                union 
            select lpad(' ',level*6)||t01.name t1, t02.deptid, rownum t3 from t01,t02,t03 
                    where t02.deptid=t03.depid and t01.lxid=t03.lxid 
                    connect by prior t03.lxid=t03.upid 
                    start with t03.upid is null
            ) order by t2 asc, t3 asc;T1                             T2 T3
    ------------------------------ -- --
    书包一                          1  0
          红文具盒                  1  1
                铅笔                1  2
                圆珠笔              1  3
          蓝文具盒                  1  4
    书包二                          2  0
    书包3                           3  0
      

  6.   

    为什么要rownum,我去掉rownum后,显示结果不全
      

  7.   

    用8楼的sql,改了下排序条件,不知道可以了吗
    select * from 
          (select name t1, deptid t2,0 t3 from t02 
                union 
            select lpad(' ',level*6)||t01.name t1, t02.deptid, t01.lxid t3 from t01,t02,t03 
                    where t02.deptid=t03.depid and t01.lxid=t03.lxid 
                    connect by prior t03.lxid=t03.upid 
                    start with t03.upid is null 
            ) order by t2 asc, t3 asc; 
      

  8.   

    8楼运行得结果
    T1                      T2         T3
    ----------------------- -- -------------
    书包一                      1           0
          红文具盒             1           1
                铅笔          1           2
                圆珠笔        1           3
    书包二                      2           0
    书包三                      3           012楼运行出错:ORA-01790: expression must have same datatype as corresponding expression
    呵呵,学习中