我要选择那些没有任何学生入学选择不具有任何学生入学
这些都段部分是三个表:
ENROLLMENTS student_id, section_id
SECTIONS course_id, section_id
COURSES course_id, description
输出表应该如下所示: course_id | description | section_id
我不舒服哪个连接使用。
我要选择那些没有任何学生入学选择不具有任何学生入学
这些都段部分是三个表:
ENROLLMENTS student_id, section_id
SECTIONS course_id, section_id
COURSES course_id, description
输出表应该如下所示: course_id | description | section_id
我不舒服哪个连接使用。
所以,你要
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;
这将需要的所有信息,从COURSES
和SECTIONS
,然后将其加入到ENROLLMENTS
,保持所有的信息COURSES
和SECTIONS
那里有一个没有注册。然后通过选择ENROLLMENTS
student_id
是NULL
的位置,我们在SECTIONS
中找到没有section_id
的所有条目。
为什么不使用子查询?
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)