1

以下是关系:

CREATE TABLE employee (
  name    varchar2(15) not null, 
  ssn      char(9),
  primary key (ssn),
  foreign key (superssn) references employee(ssn),
);

CREATE TABLE project (
  pname      varchar2(15) not null,
  pnumber    number(4),
  primary key (pnumber),
  unique (pname),
);

CREATE TABLE works_on (
  essn   char(9),
  pno    number(4),
  primary key (essn,pno),
  foreign key (essn) references employee(ssn),
  foreign key (pno) references project(pnumber)
);

问:检索参与每个项目的所有员工的姓名。

我想我被要求查询参与项目的所有员工,按项目名称排序。知道如何解决这个问题吗?

4

2 回答 2

1

这是一个经典的divide问题。

尝试这个。

   SELECT name FROM Employee 
    WHERE NOT EXISTS (SELECT pnumber FROM project WHERE pnumber   
      NOT IN (SELECT pno from Works_on WHERE employee.ssn = works_on.essn))

或者对于所有项目及其员工,

  SELECT employees.name, project.pname FROM employees 
    INNER JOIN works_on ON employees.ssn = works_on.essn
      INNER JOIN project ON works_on.pno = project.pnumber;
于 2013-04-10T19:46:15.303 回答
0
SELECT E.NAME FROM
EMPLOYEE E,
PROJECT P,
WORKS_ON WO
WHERE 
E.SSN = WO.ESSN
AND WO.PNO = P.PNUMBER
ORDER BY P.PNAME;

检查这是否对您有用。

你也可以试试这个:

SELECT E.NAME FROM
(SELECT E.NAME, P.PNAME FROM
    EMPLOYEE E,
    PROJECT P,
    WORKS_ON WO
    WHERE 
    E.SSN = WO.ESSN
    AND WO.PNO = P.PNUMBER
    ORDER BY P.PNAME);
于 2013-04-10T19:41:03.470 回答