mySql子查询答案检查期末考试问题

时间:2016-01-03 19:21:13

标签: mysql sql sql-server database

我对我的这些答案有些怀疑,如果你们能澄清我是对还是错,我将非常感激,

的问题:

员工可能被分配到多个项目,而项目可能有许多员工。请考虑以下关系模式,并为以下查询编写SQL语句。

Employees (empID, empName, empDOB, empAddress, salary, deptID, jobID)

Assignments (empID, projID, assignedDate, completionDate, status)

Projects (projID, projDescription, startDate, endDate, projType)

(a)显示1980年1月31日之前出生并分配了“办公楼”类型项目的员工姓名,按姓名的升序排序结果。 (5分)

(b)检索至少分配了两(2)个项目的empID。 (5分)

的答案:

(a) SELECT empName FROM Employees WHERE empDOB < '31-01-1980' AND projType = (SELECT projType FROM Projects WHERE projType = 'Office Complex') ORDER BY empName; 

(b) SELECT empID FROM Employees GROUP BY (SELECT projID From Projects) HAVING COUNT(*)>1 ORDER BY empID; 

我觉得第二个问题的答案可能是错误的。

1 个答案:

答案 0 :(得分:0)

对于a)部分,我加入表格以匹配具有项目类型的员工:

 SELECT empName 
 FROM Employees 
 INNER JOIN Assignments ON Assignments.empID = Employees.empID
 INNER JOIN Projects ON Assignments.projID = Projects.projID
 WHERE empDOB < 31-01-1980 AND projType = 'Office Complex' 
 ORDER BY empName;

目前,您的陈述试图找到projType =&#39; Office Complex&#39;在Employee表中,它不存在。

对于第二个问题,您需要的一切都在Assignments表中:

SELECT empID, COUNT(projID)
FROM Assignments
GROUP BY empID
HAVING COUNT(projID) > 1