因此,说我正在做类似查找在多个项目中工作的所有员工的名字和姓氏的操作。我知道最简单的方法是使用COUNT,就像这样...。
SELECT e.Fname, e.Lname
FROM employee e INNER JOIN works_on w
ON e.SSN = w.ESSN
GROUP BY SSN
HAVING COUNT(*) > 1
这可以满足我的需要,但是我正在帮助的某个人问我如何在不使用COUNT .....的情况下完成此操作,我也不知道。因此,我以为我会在这里提出任何建议或建议,因为我对此很不满意。作为参考,下面是使用过的数据库的快速浏览。人们可以提供的任何帮助或指导将不胜感激。
只是编写与Armin类似的查询和Kenney推荐的另一种方式。
create table employee(ssn int, fname varchar(50), lname varchar(50));
create table works_on (essn int, pno int);
insert into employee values (1, 'Tom', 'Smith'), (2, 'Matt', 'Smith'), (3, 'Sam', 'Smith');
insert into works_on values (1, 1), (2, 1), (2, 2), (3, 1), (3, 2);
select distinct fname, lname
from employee e
inner join works_on w1 on e.ssn = w1.essn
where exists (
select 1 from works_on where essn = e.ssn and pno <> w1.pno
);
Result:
| fname | lname |
|-------|-------|
| Matt | Smith |
| Sam | Smith |
示例:http://sqlfiddle.com/#!9 / e2411 / 5
不过,我更喜欢count
您使用的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句