2015-12-24 23 views
-1

我要选择那些没有任何学生入学选择不具有任何学生入学

这些都段部分是三个表:

ENROLLMENTS student_id, section_id

SECTIONS course_id, section_id

COURSES course_id, description

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

我不舒服哪个连接使用。

回答

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那里有一个没有注册。然后通过选择ENROLLMENTSstudent_idNULL的位置,我们在SECTIONS中找到没有section_id的所有条目。

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)