试图理解这个嵌套查询,也许这是一个错字,也是使用 having 的不同方式
trying to understand this nested query, maybe it is a typo, also different way using having
这是数据库:
EMPLOYEE (fmane, 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)
问题是:
List ssn, last and first name of all the employees who is working on project number 02 using nested query
正确答案是:
select e.ssn, e.lname, e.fname
From employee e
where eissn in (select w.essn
from works_on w
where w.essn = e.ssn and w.pno ='02'
);
1) 什么是eissn
?打字错误,如何解决?
2) 有没有其他写答案的方法,比如使用 having 或 group on?
eissn
显然是错字,因为没有这样的列。通过检查 employee
table 中的列列表,我猜它应该是 e.ssn
。如果编写查询的人使用 vi or vim,这将尤其合理,其中按 i
应该将您切换到插入模式,但如果您已经在其中,则只会添加一个 i
字符。
不过,即使修正了拼写错误,此查询仍需要一些工作。具体来说,w.essn = e.ssn
对于 in
条件是错误的。您可以在没有它的情况下使用 in
条件:
SELECT e.ssn, e.lname, e.fname
FROM employee e
WHERE e.ssn IN (SELECT w.essn
FROM works_on w
WHERE w.pno ='02'
);
或将条件转换为exists
条件:
SELECT e.ssn, e.lname, e.fname
FROM employee e
WHERE e.ssn EXISTS (SELECT *
FROM works_on w
WHERE e.ssn = w.essn AND w.pno ='02'
);
这是数据库:
EMPLOYEE (fmane, 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)
问题是:
List ssn, last and first name of all the employees who is working on project number 02 using nested query
正确答案是:
select e.ssn, e.lname, e.fname
From employee e
where eissn in (select w.essn
from works_on w
where w.essn = e.ssn and w.pno ='02'
);
1) 什么是eissn
?打字错误,如何解决?
2) 有没有其他写答案的方法,比如使用 having 或 group on?
eissn
显然是错字,因为没有这样的列。通过检查 employee
table 中的列列表,我猜它应该是 e.ssn
。如果编写查询的人使用 vi or vim,这将尤其合理,其中按 i
应该将您切换到插入模式,但如果您已经在其中,则只会添加一个 i
字符。
不过,即使修正了拼写错误,此查询仍需要一些工作。具体来说,w.essn = e.ssn
对于 in
条件是错误的。您可以在没有它的情况下使用 in
条件:
SELECT e.ssn, e.lname, e.fname
FROM employee e
WHERE e.ssn IN (SELECT w.essn
FROM works_on w
WHERE w.pno ='02'
);
或将条件转换为exists
条件:
SELECT e.ssn, e.lname, e.fname
FROM employee e
WHERE e.ssn EXISTS (SELECT *
FROM works_on w
WHERE e.ssn = w.essn AND w.pno ='02'
);