본문 바로가기

DataBase/MySQL

set

1. set

set은 집합연산을 말한다. 두 개의 select문을 통해 가져오는 결과들에 대해 합집합, 교집합, 차집합의 연산을 하여 데이터를 가져올 수 있게 한다.

집합연산을 위해서는 두 select문을 통해 가져오는 컬럼이 동일해야한다!

  • 합집합: union(중복되는 데이터는 제외), union all(중복되는 데이터를 모두 가져옴)
  • 교집합: join문을 이용한다.
  • 차집합: 두 select문에서 중복되는 부분을 제거하고, 첫번째 select 문의 결과만 가져온다. 서브쿼리문을 이용.
-- 합집합
select emp_no
from titles
where title = 'Senior staff'
union
select emp_no
from titles
where title = 'staff';

select emp_no
from titles
where title = 'Senior staff'
union all
select emp_no
from titles
where title = 'Staff';

-- 교집합
select t1.emp_no
from titles t1, titles t2
where t1.emp_no = t2.emp_no and t1.title = 'Senior Staff' and t2.title = 'Staff';

-- 차집합
select emp_no
from titles
where title = 'Staff' and emp_no not in (select emp_no from titles where title = 'Senior Staff');

 

 

 

 

[출처]윤재성의 처음 시작하는 MySQL DataBase - 인프런 | 강의 (inflearn.com)

'DataBase > MySQL' 카테고리의 다른 글

transaction  (0) 2021.08.07
데이터베이스 관리  (0) 2021.08.06
서브 쿼리문  (0) 2021.08.03
join문  (0) 2021.08.02
숫자 함수, 문자열 함수, 날짜 함수, 그룹 함수, Group by & Having 절  (0) 2021.08.01