如何修改此查询,以便在dpt 17之前仅显示所有项目中的员工姓名。
到目前为止,我有:select fname, lname from employee where ssn in
(
select distinct essn from works_on b where
b.pnum in ( select pnum from project where dnum = 17)
)
这是我创建的 [Link]
的sql小提琴的链接真的很感激任何帮助。我试图了解其运作方式背后的逻辑。
答案 0 :(得分:2)
当您逐步执行SQL时,SQL很容易。我建议您运行这些查询并确保了解其结果
部门17的第一个项目数据
SELECT *
FROM project
WHERE project.dnum = 17
现在所有参与其中的人
SELECT *
FROM project
LEFT JOIN works_on ON project.pnumber = works_on.pno
WHERE project.dnum = 17
我们如何知道work_on.essn计数等于项目总数
SELECT works_on.essn
FROM project
LEFT JOIN works_on ON project.pnumber = works_on.pno
WHERE project.dnum = 17
GROUP BY works_on.essn
HAVING count(*) = (SELECT count(*) FROM project WHERE project.dnum = 17)
容易吗?
如果包含的子查询不起作用,您可以使用这样的交叉连接
SELECT works_on.essn
FROM project
LEFT JOIN works_on ON project.pnumber = works_on.pno
CROSS JOIN (SELECT count(*) AS C FROM project WHERE project.dnum = 17) SUB
WHERE project.dnum = 17
GROUP BY works_on.essn
HAVING count(*) = MAX(SUB.C)