简体   繁体   English

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

[英]mySql Subquery Answers Check Final Exam Questions

I'm having some doubts regarding these answers of mine, would greatly appreciate if you guys can clarify if im right or wrong, 我对我的这些回答有疑问,如果你们能澄清我是对还是错,将不胜感激,

Questions: 问题:

An employee may be assigned to more than one project and a project may have many employees. 一个雇员可能被分配到多个项目,而一个项目可能有许多雇员。 Consider the following relational schema and write SQL statements for the below queries. 考虑以下关系模式,并为以下查询编写SQL语句。

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

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

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

(a) Display the names of employees who were born before 31st Jan 1980 and assigned a 'Office Complex' type project, sort results in ascending order of name. (a)显示在1980年1月31日之前出生并分配了“办公大楼”类型项目的雇员的姓名,并按姓名的升序对结果进行排序。 (5 marks) (5分)

(b) Retrieve the empIDs who are assigned at least two (2) projects. (b)检索分配了至少两(2)个项目的empID。 (5 marks) (5分)

Answers: 答案:

(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; 

I feel the answer for the second question may be wrong. 我觉得第二个问题的答案可能是错误的。

For part a), I'd join the tables to match up employees with the project type: 对于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;

As it stands, your statement attempts to find projType = 'Office Complex' in the Employee table, where it doesn't exist. 就目前而言,您的语句尝试在Employee表中查找不存在的projType ='Office Complex'。

For the second question, everything you need is in the Assignments table: 对于第二个问题,您需要的所有内容都在“工作分配”表中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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