//Connect by指定查询的逻辑
//Start with指定遍历树的根节点
Create Table Company(
eid int,
elead int,
ename varchar(8),
esalary int,
edeptno varchar(8));
insert into Company values(1,0,'king',100000,'001');---CEO
insert into Company values(2,1,'jack',50000,'002');---经理1
insert into Company values(3,1,'arise',60000,'003');--经理2
insert into Company values(4,2,'scott',30000,'002');---经理1手下员工1
insert into Company values(5,2,'tiger',25000,'002');---经理1手下员工2
insert into Company values(6,3,'wudde',23000,'003');---经理2手下员工1
insert into Company values(7,3,'joker',21000,'003');---经理2手下员工2
----分层查询:深度优先遍历
select eid,elead,ename,prior ename as lname,esalary, level loopn----循环次数(层次)看深度优先遍历
from Company
start with eid=1---指定了根结点
connect by elead = prior eid---向下查询
select eid,elead,ename,prior ename as lname,esalary,level loopn
from Company
start with elead=0
connect by elead = prior eid;
----向上查询
select eid,elead,ename as lead_name,prior ename as emp_name,esalary,level loopn
from Company
start with elead=2
connect by eid = prior elead
order by elead;
select eid,elead,ename as lead_name,prior ename as emp_name,esalary, level loopn
from Company
start with eid=2
connect by eid = prior elead
order by elead;