现有三个表
表一
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
......现在想得到如下结果
书包一
红文具盒
铅笔
圆珠笔
书包二
蓝文具盒
铅笔
......
表一
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
......现在想得到如下结果
书包一
红文具盒
铅笔
圆珠笔
书包二
蓝文具盒
铅笔
......
connect by ....语句
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
用start with... connect by ...分层语句。
(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
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;
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
呵呵,学习中