1

我的代码是

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

从事于

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
4

1 回答 1

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
    )
于 2012-12-07T03:45:14.717 回答