这是我要回答的问题:
我所拥有的是:
SELECT DISTINCT a1.acnum
FROM academic a1, academic a2
WHERE a1.deptnum = a2.deptnum
AND a1.acnum <> a2.acnum
AND a1.acnum IN (Select acnum
from interest
group by acnum
having count(acnum) >1);
这是错误的,因为我正在做的是
如果acnum(学术编号)218与acnum 217处于同一部门,并且与acnum 199(差异部门)具有相同的兴趣,则我将acnum 218添加到列表中。
但是,如果它们两个都具有相同的字段兴趣,则仅应添加218和217。
兴趣表具有fieldnum和acnum
学术表具有acnum,deptnum,名称
部门表具有deptnum,deptName
FIELDNUM ACNUM DESCRIP
------------------ --------------------
292 100 Multiprocessor and Special purpose computer design
293 100 General (HW)
293 197 Computer architecture
输出应仅列出所有学者的编号。
但要明确一点:
Acnum Deptnum Interest
1 1 g&f
2 1 g&f
3 2 f
4 3 l
5 4 r&l
6 4 r&l
输出应为:
1个
2
5
6
最佳答案
未经测试,但应该很好
SELECT DISTINCT a1.acnum
FROM academic a1
INNER JOIN academic a2 ON a1.deptnum = a2.deptnum
AND
a1.acnum <> a2.acnum
INNER JOIN interest i1 ON a1.acnum=i1.acnum
GROUP BY a1.acnum
HAVING COUNT(i1.acnum)=(SELECT COUNT(*)
FROM interest i2
WHERE i1.acnum=i2.acnum)