2015-12-24 46 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)