gpt4 book ai didi

sql - 使用子查询进行比较

转载 作者:行者123 更新时间:2023-12-04 14:42:01 25 4
gpt4 key购买 nike

这是我要回答的问题:

我有的是:

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(diff 部门)具有相同的兴趣,那么我将 acnum 218 添加到列表中。

但是,我应该只添加 acnum 218 和 217,如果它们都有相同数量的现场利益的话。

interest table has fieldnum , acnum

Academic table has acnum , deptnum, name

department table has 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)

关于sql - 使用子查询进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com