0

我有一张桌子:

courseTracker(
        student int
        courseID int
)

我想找到每个与学生“B”有相同课程的学生

4

1 回答 1

0

我不确定你想要这两个中的哪一个,所以我将两者都包括在内。

这将返回与 B 至少有一门课程相同的学生:

SELECT DISTINCT student FROM coursetracker
WHERE courseID IN (
    SELECT courseID FROM coursetracker WHERE student = 'B'
)

这将返回与 B 具有完全相同课程的学生:

SELECT student FROM (SELECT DISTINCT student FROM coursetracker) c
WHERE NOT EXISTS (
    SELECT * FROM coursetracker c1
        FULL JOIN coursetracker c2 ON c1.courseId = c2.courseId
            AND c1.student = c.student AND c2.student = 'B'
    WHERE c1.courseID IS NULL OR c2.courseID IS NULL
)
于 2013-11-03T03:35:51.897 回答