MY-SQL/교집합,합집합,차집합

[MYSQL]차집합,교집합, 합집합

정윤재 2011. 5. 19. 22:13
1. 차집합
select a.id from t1 a left join t2 b on a.id=b.id where b.id is null
=> a와 b를 outer join 한 다음 b가 null 인 값을 제거 하므로 차집합을 만듬

2. 교집합
select * from A,B wher A.id=B.id
=>이퀴조인을 써서 교집합을 만듬

3. 합집합
(select * from USER_TBL where USER_ID='shonm')
union
(select * from USER_TBL where USER_ID='shonm')
=> union 을 쓰면 중복된 데이터는 나오지 않음 distinct 효과
     그러나 union all 을 쓰면 중복된 데이터도 나오게 됨
     위의 예제에서 union 을 쓰면 1 개 row 가 결과, union all 을 쓰면 2개 row 가 결과임