简体   繁体   中英

how can i get different data from three tables?

Query:

select te.empno, te.ename, te.mgr
from test te
where te.deptno=10 and
      not exists (select 1 from t1 te1 where te1.deptno=te.deptno and te1.empno=te.empno) and
      not exists (select 1 from t te2 where te2.mgr=te.mgr);

table_name:te

EMPNO   ENAME   JOB      MGR    HIREDATE    SAL COMM    DEPTNO
7839    KING    PRESIDENT   -   17-Nov-81   5000    -   10
7782    CLARK   MANAGER  7839   9-Jun-81    2450    -   10
7934    MILLER  CLERK    7782   23-Jan-82   1300    -   10
123    Ranga    -        7566   -              -    -   10
124    srinu    -         123   -             - -       10

table_name:t1

EMPNO   DEPTNO
7782    10
7934    10
7839    10

table_name:t

MGR     NAME
7566    aaa

Here I have three different tables. I want to required three tables different data. Is it possible to get the different please help on this?

Required output:

EMPNO   ENAME   JOB      MGR    HIREDATE    SAL COMM    DEPTNO
123     Ranga    -        7566   -              -    -   10
124     srinu    -         123   -             - -       10
select te.empno, te.ename, te.mgr from test te
WHERE te.deptno=10
AND not exists(select 1 from t1 te1 where te1.deptno=te.deptno and te1.empno=te.empno)
AND  exists(select 1 from t te2 where te2.mgr=te.mgr)
union
select te.empno, te.ename, te.mgr from test te
WHERE te.deptno=10
AND  not exists(select 1 from t1 te1 where te1.deptno=te.deptno and te1.empno=te.empno)
AND not  exists(select 1 from t te2 where te2.mgr=te.mgr)

this is for the solution above the question

I think that you only need two tables. if i understood correctly you only need two tables.

select te.EMPNO, te.ENAME, te.JOB, t.MGR,t.NAME as "MGRNAME",te.HIREDATE, te.SAL, te.COMM, te.DEPNO
from te
join t on t.MGR=te.MGR

OR if you need to use the 3 tables:

select te.EMPNO, te.ENAME, te.JOB, t.MGR,t.NAME as "MGRNAME",te.HIREDATE, te.SAL, te.COMM, te.DEPNO
from te
join t1 on t1.EMPNO=te.EMPNO and t1.DEPNO=te.DEPTNO
join t on t.MGR=te.MGR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM