[英]Left Outer Join in sql query
The database: 数据库:
EMPLOYEE (fname, minit, lname, ssn, birthdate, address, sex, salary, superssn, dno) KEY: ssn
DEPARTMENT (dname, dnumber, mgrssn, mgrstartdate) KEY: dnumber.
PROJECT (pname, pnumber, plocation, dnum) KEY: pnumber.
WORKS_ON (essn, pno, hours) KEY: (essn, pno)
DEPENDENT (essn, dependent-name, sex, bdate, relationship) KEY: (essn, dependent-name)
I want to use left outer join
and group by
to... 我想使用left outer join
和group by
...
Find the last name and SSN of those managers who work on 3 or more projects and who are not located in Cleveland. 查找从事3个或更多项目且不在克利夫兰的经理的姓氏和SSN。
Here is what I have so far: 这是我到目前为止的内容:
select Lname
from Employee e outer join Department d
where (e.ssn = d.mgrssn)
and ssn NOT in (
select w.essn
from works_on w outer join Project p
where w.pno = p.pnumber
and p.plocation = 'Cleveland'
group by w.essn
having count(*) >= 3
)
Did I do it right using left outer join
and group by
? 我是否使用left outer join
和group by
做对group by
? Should I divide this code into two parts, like loops? 是否应将此代码分为两个部分,例如循环?
Select JOIN
find all project for the employee 选择JOIN
查找员工的所有项目
First HAVING
tell you this user doesnt have project in 'Cleveland'
首先, HAVING
告诉您此用户在'Cleveland'
没有项目
Second HAVING
tell you this user has 3 project or more 第二个HAVING
告诉您该用户有3个项目或更多
. 。
SELECT e.Lname, e.ssn
FROM Employee e
JOIN works_on w
ON e.ssn = w.essn
JOIN Project p
ON w.pno = p.pnumber
GROUP BY e.ssn
HAVING
SUM(CASE WHEN p.plocation = 'Cleveland' THEN 1 ELSE 0 END) = 0
AND COUNT(*) >= 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.