列出由他/她的部门赞助的一个项目的员工姓名,并处理一个非他/她部门赞助的项目

时间:2012-12-07 03:00:59

标签: sql oracle11g

我的代码是

select name
from employee e, workon w
where e.empid = w.empid
and pid in
   (select pid 
    from workon
    where did in
         (select did
          from employee ee
          where e.did = ee.did))
group by name

但我知道这是不对的,因为我还需要找到一个在他的部门以外的项目中工作的人。问题是我不太清楚该怎么做。

表 雇员

EMPID   NAME    SALARY  DID
1   kevin   32000   2
2   joan    46200   1
3   brian   37000   3
4   larry   82000   5
5   harry   92000   4
6   peter   45000   2
7   peter   68000   3
8   smith   39000   4
9   chen    71000   1
10  kim 46000   5
11  smith   46000   1

Workon

PID EMPID   HOURS
3   1   30
2   3   40
5   4   30
6   6   60
4   3   70
2   4   45
5   3   90
3   3   100
6   8   30
4   4   30
5   8   30
6   7   30
6   9   40
5   9   50
4   6   45
2   7   30
2   8   30
2   9   30
1   9   30
1   8   30
1   7   30
1   5   30
1   6   30
2   6   30
Project 

PID PNAME   BUDGET  DID
1   DB development  8000    2
2   network development 6000    2
3   Web development 5000    3
4   Wireless development    5000    1
5   security system 6000    4
6   system development  7000    1

1 个答案:

答案 0 :(得分:0)

select e.name
from employee e
where
    -- Projects in department
    exists (
        select *
        from 
            workon w
            join project p
                on w.pid = p.pid
                and p.did = e.did
        where w.empid = e.empid
    )
    -- Projects out of department
    and exists (
        select *
        from 
            workon w
            join project p
                on w.pid = p.pid
                and p.did != e.did
        where w.empid = e.empid
    )