λŒ€μž₯μΏ΅μ•Ό 2023. 3. 26. 14:33

관계 λŒ€μˆ˜ μ—°μ‚°
    1. μ…€λ ‰μ…˜ > select where
    2. ν”„λ‘œμ μ…˜ > select column
    3. μ‘°μΈ > a join b
    4. ν•©μ§‘ν•©, μ°¨μ§‘ν•©, κ΅μ§‘ν•©
    
    union
    - ν•©μ§‘ν•©
    
    intersect
    - κ΅μ§‘ν•©
 
    minus
    - μ°¨μ§‘ν•©

 

-- 쑰인: 컬럼 + 컬럼
-- μœ λ‹ˆμ˜¨: λ ˆμ½”λ“œ + λ ˆμ½”λ“œ

select * from tblMen 
union 
select * from tblWomen;

-- νšŒμ‚¬ λΆ€μ„œ > κ²Œμ‹œνŒ
select * from κ²Œμ‹œνŒ; --4천만건

select * from μ˜μ—…λΆ€κ²Œμ‹œνŒ; --천만건
select * from μ΄λ¬΄λΆ€κ²Œμ‹œνŒ; --2천만건
select * from κ°œλ°œλΆ€κ²Œμ‹œνŒ; --천만건

-- 사μž₯λ‹˜ > λͺ¨λ“  λΆ€μ„œμ˜ κ²Œμ‹œνŒ > ν•œλ²ˆμ— μ—΄λžŒ~
select * from μ˜μ—…λΆ€κ²Œμ‹œνŒ
union
select * from μ΄λ¬΄λΆ€κ²Œμ‹œνŒ
union
select * from κ°œλ°œλΆ€κ²Œμ‹œνŒ;


-- SNS > κ²Œμ‹œλ¬Ό > λ…„λ„λ³„λ‘œ~ > μ‹Έμ΄μ›”λ“œ

select * from κ²Œμ‹œνŒ2020
union
select * from κ²Œμ‹œνŒ2021
union
select * from κ²Œμ‹œνŒ2022
union
select * from κ²Œμ‹œνŒ2023 where 검색; --where절 > λͺ¨λ“  union에 λŒ€ν•œ 검색

-- 쑰건 > μŠ€ν‚€λ§ˆ(컬럼 μˆœμ„œ, 개수, μžλ£Œν˜•)κ°€ 동일해야 ν•œλ‹€.
-- 쑰건 > 데이터 μ„±μ§ˆ λ™μΌν•΄μ•Όν•œλ‹€.
select * from tblCountry --5개 컬럼
union
select * from tblInsa; --10개 컬럼

-- 개수, μžλ£Œν˜• κ°™μœΌλ©΄ 물리적으둜 union 됨(의미X)
select name, capital, population from tblCountry
union
select name, buseo, basicpay from tblInsa;




create table tblUnionA (
    name varchar2(30) not null
);

create table tblUnionB (
    name varchar2(30) not null
);

insert into tblUnionA values ('κ°•μ•„μ§€'); --*
insert into tblUnionA values ('고양이'); --*
insert into tblUnionA values ('토끼');
insert into tblUnionA values ('거뢁이');
insert into tblUnionA values ('병아리');

insert into tblUnionB values ('ν˜Έλž‘μ΄');
insert into tblUnionB values ('μ‚¬μž');
insert into tblUnionB values ('κ°•μ•„μ§€'); --*
insert into tblUnionB values ('코끼리');
insert into tblUnionB values ('고양이'); --*

select * from tblUnionA;
select * from tblUnionB;

-- union > μˆ˜ν•™ μ§‘ν•©μ˜ κ°œλ… > ν•©μ§‘ν•© > 쀑볡값 ν—ˆμš©X
select * from tblUnionA
union
select * from tblUnionB; --결과 8마리

-- union all > 쀑볡값 ν—ˆμš©O
select * from tblUnionA
union all
select * from tblUnionB; --결과 10마리

-- intersect > ꡐ집합
select * from tblUnionA
intersect
select * from tblUnionB; --결과 2마리

-- minus > μ°¨μ§‘ν•©
select * from tblUnionA
minus
select * from tblUnionB; --결과 3마리