Union

Jane의 study note.·2022년 12월 2일
0

MySQL

목록 보기
8/17

실습환경 만들기

  1. zerobase 데이터베이스로 이동
use zerobase;
select * from celeb;
  1. 테스트용 테이블 2개 생성
create table test1
(
    no int
);

create table test2
(
    no int
);
  1. 테스트 데이터 추가
insert into test1 values(1);
insert into test1 values(2);
insert into test1 values(3);
=> 04. DML 중 2.insertinto 뒤에 테이블명 (컬럼여러개 명시);

insert into test2 values(5);
insert into test2 values(6);
insert into test2 values(3);
  1. 데이터 확인
select * from test1;
select * from test2;

Union 문법

union all : 겹치기 있음(중복값 있음)
union: 겹치기 없음(중복값 없음)

  1. UNION 문법
    여러개의 SQL문을합쳐서하나의 SQL 문으로만들어주는방법 (주의. 칼럼의개수가같아야함)
  • UNION : 중복된값을제거하여알려준다.
  • UNION ALL : 중복된값도모두보여준다.
  1. UNION 예제 1 - UNION ALL, UNION (p13~14)
    test1 의모든데이터와 test2 의모든데이터를중복된값을포함하여검색
select * from test1
union all
select * from test2;

|    1 |
|    2 |
|    3 |
|    5 |
|    6 |
|    3 | 출력


select * from test1
union
select * from test2;

|    1 |
|    2 |
|    3 |
|    5 |
|    6 | 출력

3 UNION 예제 2 - UNION ALL, UNION (p15~16)
성별이여자인 데이터를 검색하는 쿼리와 소속사가 YG엔터테이먼트인데이터를검색하는쿼리를 UNION ALL으로실행

select name, sex, agency from celeb where sex='F'
UNION ALL
select name, sex, agency from celeb where agency ='YG엔터테이먼트';

| name   | sex  | agency           |
+--------+------+------------------+
| 아이유 | F    | EDAM엔터테이먼트 |
| 이미주 | F    | 울림엔터테이먼트 |
| 이수현 | F    | YG엔터테이먼트   |
| 강동원 | M    | YG엔터테이먼트   |
| 차승원 | M    | YG엔터테이먼트   |
| 이수현 | F    | YG엔터테이먼트   |
=> 이수현이 2번 출력됨

성별이여자인데이터를검색하는쿼리와소속사가 YG엔터테이먼트인데이터를검색하는쿼리를 UNION 으로실행

select name, sex, agency from celeb where sex='F'
UNION
select name, sex, agency from celeb where agency ='YG엔터테이먼트';

| name   | sex  | agency           |
+--------+------+------------------+
| 아이유 | F    | EDAM엔터테이먼트 |
| 이미주 | F    | 울림엔터테이먼트 |
| 이수현 | F    | YG엔터테이먼트   |
| 강동원 | M    | YG엔터테이먼트   |
| 차승원 | M    | YG엔터테이먼트   |
+--------+------+------------------+
  1. UNION 예제 3 - 1~4 (p17~20)
    가수가 직업인 연예인의 이름, 직업을 검색 하는 쿼리와,
    1980년대에 태어난연예인의이름, 생년월일, 나이를검색하는쿼리를 UNION으로실행
select name, job_title from celeb where job_title like '%가수%'
union
select name, birthday, age from celeb where birthday between 19800101 and 19891231;
=> 오류: ERROR 1222 (21000): The used SELECT statements have a different number of columns, 유니온하려는 두 테이블의 컬럼이 같아야함

select name, birthday, job_title, age from celeb where job_title like '%가수%'
union
select name, birthday, job_title, age from celeb where birthday between 19800101 and 19891231;

강동원도 탤런트-> 텔런트로 바꿔놓기
update celeb
set job_title='영화배우, 텔런트'
where name='강동원';

select * from celeb;

문제 2(p23~24)
- 직업이가수인 (가수를포함하는) 데이터를검색하는쿼리와 직업이텔런트인 (텔런트를포함하는) 데이터를검색하는쿼리를 중복을제거하여합쳐서실행하세요.

select name, job_title from celeb where  job_title like '%가수%'
union
select name, job_title from celeb where  job_title like '%텔런트%';

- 성이이씨인데이터를검색하는쿼리와 1970년대생을검색하는쿼리를 중복을포함하여합쳐서실행하세요.  
select name, birthday from celeb where  name like '이%'
union all
select name, birthday from celeb where  birthday between 19700101 and 19791231;

※ 컬럼명이 brithday로 오타났을 때 고치는 법
=> 03. DDL_Table 을 사용해보자 (CREATE, ALTER, DROP) 3-1)-#4
alter table celeb 
chage column brithday birthday date;
desc celeb;

ALTER TABLE person CHANGE COLUMN agee age int;
desc person;

0개의 댓글