左外加入 sql 查询
Left Outer Join in sql query
数据库:
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)
我想使用 left outer join
和 group by
来...
查找从事 3 个或更多项目且不在克利夫兰的经理的姓氏和 SSN。
这是我目前的情况:
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
)
我使用 left outer join
和 group by
做对了吗?我应该把这段代码分成两部分,就像循环一样吗?
Select JOIN
查找员工的所有项目
首先HAVING
告诉你这个用户在'Cleveland'
中没有项目
第二个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
数据库:
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)
我想使用 left outer join
和 group by
来...
查找从事 3 个或更多项目且不在克利夫兰的经理的姓氏和 SSN。
这是我目前的情况:
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
)
我使用 left outer join
和 group by
做对了吗?我应该把这段代码分成两部分,就像循环一样吗?
Select JOIN
查找员工的所有项目
首先HAVING
告诉你这个用户在'Cleveland'
第二个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