我将从表格的简化示例开始:
+-----+----------+
|Name |Teaches |
+-----+----------+
|Dave |Science |
+-----+----------+
|Dave |History |
+-----+----------+
|Alice|History |
+-----+----------+
|Alice|Literature|
+-----+----------+
|Alice|Science |
+-----+----------+
|John |History |
+-----+----------+
我正在尝试选择那些与戴夫教授相同类(class)的人。 (在这种情况下,爱丽丝)。我正在考虑使用光标浏览 Dave 的类(class),选择教授同一类(class)的人员并交叉结果,但我想知道是否有更好(更简单)的方法。
最佳答案
这是一种方法:
select t.name
from t join
t td
on td.teaches = t.teaches
where td.name = 'Dave'
group by t.name
having count(*) = (select count(*) from t where t.name = 'Dave');
关于SQL - 如何选择具有相同多个值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179515/