选择没有任何学生注册的部分

时间:2015-12-24 11:25:38

标签: sql join oracle11g

我想选择所有没有注册学生的部分

以下是三个表格:

ENROLLMENTS student_id, section_id

SECTIONS course_id, section_id

COURSES course_id, description

输出表应如下所示: course_id | description | section_id

加入使用我并不是一件好事。

2 个答案:

答案 0 :(得分:0)

所以你想要

SELECT
    course_id
    , description
    , section_id
FROM
    COURSES
INNER JOIN
    SECTIONS
ON
    COURSES.course_id = SECTIONS.course_id
LEFT JOIN
    ENROLLMENTS
ON
    ENROLLMENTS.section_id = SECTIONS.section_id
WHERE
    ENROLLMENTS.student_id IS NULL;

这将获取COURSESSECTIONS的所有信息,然后将其加入ENROLLMENTS,同时保留COURSESSECTIONS的所有信息哪里没有注册。然后,通过选择ENROLLMENTS student_id NULL的位置,我们会找到section_id中没有SECTIONS的所有条目。

答案 1 :(得分:0)

为什么不使用子查询?

SELECT
    sections.course_id,
    courses.description,
    sections.section_id
FROM
    courses INNER JOIN sections ON courses.course_id = sections.course_id
WHERE
    courses.section_id NOT IN (SELECT section_id FROM enrollments)
相关问题