我的代码如下:
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname = 'EUR' AND p.profname != 'Peter'
INTERSECT
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname = 'SEK'
INTERSECT
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname = 'SIT'
可以使用select语句收集价值EUR,SEK,SIT:
SELECT c2.commname AS CNAME FROM committee AS c2 WHERE c2.profname = 'Peter'
所以我的问题是:如何将select语句与我的代码结合起来?
更新
这是我创建表格和插入值的查询
create table department
(
deptname varchar(20) primary key,
building varchar(20)
)
go
create table professor
(
profname varchar(20) primary key,
deptname varchar(20) foreign key references department(deptname)
)
go
create table committee
(
id int identity(1,1) primary key,
commname varchar(20) not null,
profname varchar(20) foreign key references professor(profname)
)
go
insert into department (deptname, building) values ('Chad', 'Idaho');
insert into department (deptname, building) values ('ElSalvador', 'Nebraska');
insert into department (deptname, building) values ('SaintLucia', 'Kentucky');
insert into department (deptname, building) values ('Iceland', 'Vermont');
insert into department (deptname, building) values ('Spain', 'Nevada');
insert into department (deptname, building) values ('NewCaledonia', 'Nebraska');
insert into department (deptname, building) values ('PalestinianTerritory', 'Ohio');
insert into department (deptname, building) values ('PuertoRico', 'Hawaii');
insert into department (deptname, building) values ('EquatorialGuinea', 'Delaware');
go
insert into professor (profname, deptname) values ('Mark', 'Iceland');
insert into professor (profname, deptname) values ('Jane', 'Iceland');
insert into professor (profname, deptname) values ('Amanda', 'PalestinianTerritory');
insert into professor (profname, deptname) values ('Thomas', 'Spain');
insert into professor (profname, deptname) values ('Roger', 'PuertoRico');
insert into professor (profname, deptname) values ('Tammy', 'Iceland');
insert into professor (profname, deptname) values ('Phillip', 'Iceland');
insert into professor (profname, deptname) values ('George', 'SaintLucia');
insert into professor (profname, deptname) values ('Amy', 'NewCaledonia');
insert into professor (profname, deptname) values ('Shirley', 'PalestinianTerritory');
insert into professor (profname, deptname) values ('Peter', 'Iceland');
go
insert into committee (commname, profname) values ('SEK', 'Amy');
insert into committee (commname, profname) values ('QAR', 'Tammy');
insert into committee (commname, profname) values ('XAU', 'Amanda');
insert into committee (commname, profname) values ('MTL', 'George');
insert into committee (commname, profname) values ('SIT', 'Shirley');
insert into committee (commname, profname) values ('NZD', 'Shirley');
insert into committee (commname, profname) values ('EUR', 'Jane');
insert into committee (commname, profname) values ('EUR', 'Amy');
insert into committee (commname, profname) values ('EUR', 'George');
insert into committee (commname, profname) values ('EUR', 'Peter');
insert into committee (commname, profname) values ('SIT', 'Peter');
insert into committee (commname, profname) values ('SEK', 'Peter');
insert into committee (commname, profname) values ('SEK', 'George');
insert into committee (commname, profname) values ('SIT', 'George');
go
我希望找到与彼得一起去同一委员会的所有教授 在这种情况下,它应该返回 George
答案 0 :(得分:0)
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname in (SELECT c2.commname AS CNAME FROM committee AS c2 WHERE c2.profname = 'Peter') AND p.profname != 'Peter'
INTERSECT
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname = 'SEK'
INTERSECT
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE c.commname = 'SIT'
这就是你应该怎么做的。
答案 1 :(得分:0)
尝试此查询。我想你的意思是我们需要profnames
与commname
同一组Peter
的所有HAVING
。在此查询commname
中,当前profname
的{{1}}计数与profname = 'Peter'
相同
SELECT p.profname
FROM professor AS p
JOIN committee c ON c.profname = p.profname
WHERE p.profname != 'Peter'
AND
c.commname IN (SELECT DISTINCT c2.commname AS CNAME
FROM committee AS c2
WHERE c2.profname = 'Peter')
GROUP BY p.profname
HAVING COUNT(DISTINCT c.commname) =
(SELECT COUNT(DISTINCT(c2.commname))
FROM committee AS c2
WHERE c2.profname = 'Peter'
)