[DB] SET Operator

1. SET Operator

Operator설명중복
UNION합집합제거
UNION ALL합집합유지
INTERSECT교집합없음
MINUS차집합없음

mysql은 UNION, UNION ALL 연산자는 제공한다.

단, MINUS, INTERSECT 연산자는 지원하지 않는다.

그래서 동일한 기능을 다른 방식으로 구현해야 한다.


2. INTERSECT 극복

select col_1
from table_1
intersect
select col_2
from table_2;
Code language: JavaScript (javascript)

동일한 기능을 mysql에서는 inner join으로 극복해야 한다.

select col_1
from table_1 t1 inner join table_2 t2
on t1.col_1 = t2.col_2;
Code language: JavaScript (javascript)

3. MINUS 극복

select col_1
from table_1
minus
select col_2
from table_2;
Code language: JavaScript (javascript)

동일한 기능을 mysql에서는 not in, not exists, left outer join으로 극복해야 한다.

  • not in
select col_1
from table_1
where not in (
    select col_2
    from table_2
);
Code language: JavaScript (javascript)

  • not exists
select col_1
from table_1 t1
where not exists (
    select col_2
    from table_2 t2
    where t1.col_1 = t1.col_2
);
Code language: JavaScript (javascript)

  • left outer join
select col_1
from table_1 t1
left join table_2 t2
on t1.col_1 = t2.col_2
where t2.col_2 is null;
Code language: JavaScript (javascript)

댓글 남기기