use zerobase;
select * from celeb;
create table test1
(
no int
);
create table test2
(
no int
);
insert into test1 values(1);
insert into test1 values(2);
insert into test1 values(3);
=> 04. DML 중 2.insert문 into 뒤에 테이블명 (컬럼여러개 명시);
insert into test2 values(5);
insert into test2 values(6);
insert into test2 values(3);
select * from test1;
select * from test2;
union all : 겹치기 있음(중복값 있음)
union: 겹치기 없음(중복값 없음)
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엔터테이먼트 |
+--------+------+------------------+
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;