我想使用JOIN
运算符将结果带到这里,这是我的代码:
create table emp(E_id integer, e_name varchar(100), d_ID integer , primary key(e_id));
create table dep (d_ID integer, d_name varchar(100), manager_id integer ,primary key(d_ID) , FOREIGN KEY (manager_id) REFERENCES emp(E_id));
alter table emp add FOREIGN KEY(d_ID) references dep(d_ID);
insert into dep values(11,'computer',1);
insert into dep values(12,'commerce',2);
insert into dep values(13,'technology',3);
insert into emp values(1,'vishal',11) ;
insert into emp values(2,'sachin',12) ;
insert into emp values(3,'deepal',13) ;
insert into emp values(4,'sumit',11) ;
insert into emp values(5,'vinay',11) ;
insert into emp values(6,'ravish',14) ;
我想用像vinay结果的经理这样的联合运算符来搜索特定雇员的经理 我使用了嵌套查询:
select e_name
from emp
where e_id in (select manager_id
from dep
where dep.d_id in(select d_ID
from emp
where emp.e_name ='sumit'));
我得到了正确的结果,但是当我使用JOIN
运算符时,它不起作用,它会向我显示结果
vishal
sachin
deepal
我在这里看到的结果是代码:
select e_name
from emp natural join dep
where dep.manager_id=emp.e_id and emp.e_name='sumit';
答案 0 :(得分:1)
您需要将emp
加入dep
,然后再次回到emp
:
select e.e_name
from emp join dep
on dep.d_id = emp.d_id
inner join emp e
on e.e_id = dep.manager_id
where emp.e_name='sumit';
请参见demo
答案 1 :(得分:0)
请勿使用natural join
!它选择join
条件。您只需要一个inner join
:
select e.e_name
from emp e inner join
dep d
on d.manager_id = e.e_id;