我需要一些我需要编写的查询帮助
检索处理不受其部门控制的项目的员工的名字和姓氏。还显示项目的名称,员工的部门编号以及项目控制部门的编号。 (所有这些都应该显示在相同的结果表中。)以升序的alpha顺序显示结果(按姓氏,然后是名字,然后 项目名称)。
表:
Employee(Fname,Lname,SSN,DepartNum)
Department(Dname,Dnumber)
Project(Pname,Pnumber,DepNum)
WorksOn(EmpSSN,ProjNum)
查询的列标题:Fname,Lname,Pname,E_DNUM,P_DNUM
SELECT Fname,Lname,Project.Pname,(Employee.DepartNum) AS E_DNUM (Project.DepNum) AS P_DNUM
FROM Department,
(SELECT *
FROM Employee
LEFT JOIN WorksOn
LEFT JOIN Project
ON Project.Pnumber = WorksOn.Pnum
ON Employee.SSN = WorksOn.ESSN)
WHERE Project.DepNum = Department.Dnumber
AND Employee.DepartNum <> Department.Dnumber;
答案 0 :(得分:3)
如果我没有遗漏某些内容,您可以加入所有表并应用条件Employee.DepartNum <> Project.DepNum
来获得您想要的结果:
select Fname,Lname,Pname, Employee.DepartNum E_DNUM,Project.DepNum P_DNUM
from Employee
join WorksOn on WorksOn.EmpSSN = Employee.SSN
join Project on WorksOn.ProjNum = Project.Pnumber
join Department on Project.DepNum = Department.Dnumber
where Employee.DepartNum <> Project.DepNum