我必须在 SQL 中找到 2 个查询中的交集,并且我正在使用 INTERSECTION,但是当我在命令上尝试它时,当我知道有一行属于该类别时,查询不会显示任何结果。 问题如下: 查找同时修读 CIS 类(class)和历史类(class)的学生的全名 这是我的代码:
SELECT student.sid
FROM student
JOIN registration
ON registration.sid = student.sid
JOIN course
ON registration.crn = course.crn
WHERE dept= "CIS"
INTERSECT
SELECT student.sid
FROM student
JOIN registration
ON registration.sid = student.sid
JOIN course
ON registration.crn = course.crn
WHERE dept = "HIS";
最佳答案
这有效
SELECT fName, lName
FROM student
JOIN registration
ON registration.sid = student.sid
JOIN course
ON registration.crn = course.crn
WHERE dept= "CIS"
AND student.sid IN
(SELECT student.sid
FROM student
JOIN registration
ON registration.sid = student.sid
JOIN course
ON registration.crn = course.crn
WHERE dept = "HIS");
关于mysql - sql中的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055020/