괴발개발/Spring Framework

mysql쿼리문: 차집합

moonday 2021. 9. 17. 23:13

학생리스트 중에서 49번 고유번호id를 가진 선생님이 담당하는 모든 학생리스트 중에서 

수업 유형번호id가 등록된 학생을 거르기

select * from (select * from forfree.student where teacher_student_id = 49) A left outer join
=> 49번번호를 선생님으로 가진 모든 학생들을 A집단, left outer join 

(select * from forfree.class join subject on subject_id = subject_class_id join student on student_class_id = student_id where teacher_student_id = 49 ) B
=> 49번 번호를 선생님으로 가진 학생집단 중에서 수업 유형번호가 등록된 학생들 B집단,


on A.student_id = B.student_id where B.student_id is null;
=>선생님번호 49를 가진 것중에 수업유형이 비어있는 학생리스트만 나옴

on A.student_id = B.student_id where B.student_id is not null;
=>선생님번호 49를 가진 것중에 수업유형이 비어있지 않은 학생리스트만 나옴


    where teacher_student_id = 49 and subject_id =4) B on A.student_id = B.student_id
   where B.student_id is not null;
=> 49번번호를 선생님으로 가지고 수업유형이 4번인 학생만 나옴 (4번유형 값이 비지 않은 not null인)

    where teacher_student_id = 49 and subject_id =4) B on A.student_id = B.student_id
where B.student_id is null;
=>49번번호를 선생님으로 가지고 수업유형이 4번이 아닌 학생 (null) 만 나옴

 

 

*번외 (합집합) => 얘를 위에 대입하면 작동이 안되더라구..?

-- 49번을 선생님으로 갖는 모든 학생들 번호

(select student_id from student where teacher_student_id = 49)
union
(select student_class_id as student_id from class);

 

 

 

 

참고: https://huskdoll.tistory.com/558