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);
INSERT INTO test2 VALUES (5);
INSERT INTO test2 VALUES (6);
INSERT INTO test2 VALUES (3);
select from test1;
select from test2;
SELECT col1, col2, ... FROM tableA
UNION | UNION ALL
SELECT col1, col2, ... FROM tableB;
> select * from test1
UNION ALLselect from test1
UNION
select from test2;
select from celeb where sex='F'
UNION ALL
select from celeb where agency='YG엔터테이먼트';
SELECT name, job_title FROM celeb WHERE job_title LIKE '%가수%'
UNION
SELECT name, birthday, age FROM celeb WHERE birthday BETWEEN '1980-01-01' AND '1989-12-31';
select from celeb where job_title LIKE '%가수%'
UNION
select from celeb where job_title LIKE '%탤런트%';
select from celeb where name LIKE '이%'
UNION ALL
select from celeb where birthday BETWEEN '1970-01-01' AND '1980-12-31';
use zerobase;
select * from celeb;
CREATE TABLE snl_show
(
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
SEASON int NOT NULL,
EPISODE int NOT NULL,
BROADCAST_DATE date,
HOST varchar(32) NOT NULL
);
desc snl_show;
INSERT INTO snl_show VALUES (1, 8, 7, '2020-09-05', '강동원');
INSERT INTO snl_show VALUES (2, 8, 8, '2020-09-12', '유재석');
INSERT INTO snl_show VALUES (3, 8, 9, '2020-09-19', '차승원');
INSERT INTO snl_show VALUES (4, 8, 10, '2020-09-26', '이수현');
INSERT INTO snl_show VALUES (5, 9, 1, '2020-09-04', '이병헌');
INSERT INTO snl_show VALUES (6, 9, 2, '2020-09-11', '하지원');
INSERT INTO snl_show VALUES (7, 9, 3, '2020-09-18', '제시');
INSERT INTO snl_show VALUES (8, 9, 4, '2020-09-25', '조정석');
INSERT INTO snl_show VALUES (9, 9, 5, '2020-10-02', '조여정');
INSERT INTO snl_show VALUES (10, 9, 6, '2020-10-09', '옥주현');
두 개 이상의 테이블을 결합하는 것
INNER JOIN
* 두 개의 테이블에서 공통된 요소들을 통해 결합하는 조인방식
select col1, col2, ...
from tableA
INNER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
- 예제
* snl_show에 호스트로 출연한 celeb을 기준으로 celeb 테이블과 snl_show 테이블을 INNER JOIN
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
INNER JOIN snl_show
ON celeb.name=snl_show.host;
LEFT JOIN
* 두 개의 테이블에서 공통영역을 포함하고 왼쪽 테이블의 다른 데이터롤 포함하는 조인방식
select col1, col2, ...
from tableA #얘가 LEFT table
LEFT JOIN tableB # RIGHT table
ON tableA.column = tableB.column
WHERE condition;
- 예제
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
LEFT JOIN snl_show
ON celeb.name=snl_show.host;
RIGHT JOIN
* 두 개의 테이블에서 공통영역을 포함하고 오른쪽 테이블의 다른 데이터롤 포함하는 조인방식
select col1, col2, ...
from tableA #얘가 LEFT table
RIGHT JOIN tableB # RIGHT table
ON tableA.column = tableB.column
WHERE condition;
- 예제
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
RIGHT JOIN snl_show
ON celeb.name=snl_show.host;
FULL OUTER JOIN
* 두 개의 테이블에서 공통영역을 포함하여 양쪽 테이블의 다른영역을 모두 포함하는 조인방식
select col1, col2, ...
from tableA #얘가 LEFT table
FULL OUTER JOIN tableB # RIGHT table
ON tableA.column = tableB.column
WHERE condition;
- 예제 => ERROR 발생! 문법 오류
* MySQL에서는 FULL OUTER JOIN 문법 지원하지 X
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
FULL OUTER JOIN snl_show
ON celeb.name=snl_show.host;
- 대체 문법
select col1, col2, ...
from tableA #얘가 LEFT table
LEFT JOIN tableB ON tableA.column = tableB.column
UNION
select col1, col2, ...
from tableA #얘가 LEFT table
RIGHT JOIN tableB ON tableA.column = tableB.column
WHERE condition;
- 예제
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
LEFT JOIN snl_show ON celeb.name=snl_show.host
UNION
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
RIGHT JOIN snl_show ON celeb.name=snl_show.host;
SELF JOIN
* 가장 많이 사용
select col1, col2, ...
from tableA, tableB, ... # Table이 여러 개 명시
where condition;
SELECT celeb.id, celeb.name, snl_show.id, snl_show.host
FROM celeb, snl_show
WHERE celeb.name = snl_show.host; # JOIN 기준 제시
select celeb.name, celeb.job_title
from celeb, snl_show
where celeb.name=snl_show.host AND celeb.agency='안테나';
* 예제3 => 괄호의 중요성
select celeb.name, celeb.age, celeb.job_title, celeb.agency, snl_show.season, snl_show.episode
from celeb, snl_show
where celeb.name=snl_show.host AND ((NOT celeb.job_title LIKE'%영화배우%' AND celeb.agency='YG엔터테이먼트') OR (celeb.age>=40 AND celeb.agency!='YG엔터테이먼트'));
* 예제4
select snl_show.id, snl_show.season, snl_show.episode, snl_show.host, celeb.job_title
from celeb, snl_show
where celeb.name=snl_show.host;
select snl_show.id, snl_show.broadcast_date, snl_show.episode, snl_show.host
from celeb, snl_show
where celeb.name=snl_show.host AND ((snl_show.episode IN (7,9,10) OR celeb.agency LIKE 'YG__') AND (snl_show.broadcast_date>'2020-09-15'));
snl_show에 출연한 celeb 테이블 연예인 중, 영화배우나 탤런트가 아닌 연예인의 아이디, 이름, 직업, 시즌, 에피소드 정보 검색
select snl_show.id, snl_show.host, celeb.job_title, snl_show.season, snl_show.episode
from celeb, snl_show
where celeb.name=snl_show.host AND NOT (celeb.job_title LIKE '%배우%' OR celeb.job_title LIKE '%연예인%');
snl_show에 출연한 celeb 중, 작년 9월 15일 이후에 출연했거나 소속사 이름이 '엔터테이먼트'로 끝나지 않으면서 영화배우나 개그맨이 아닌 연예인의 celeb 아이디, 이름, 직업, 소속사를 검색
select celeb.id, celeb.name, celeb.job_title, celeb.agency
from celeb, snl_show
where celeb.name=snl_show.host AND (snl_show.broadcast_date>'2020-09-15' OR NOT celeb.agency LIKE '%엔터테이먼트') AND NOT (celeb.job_title LIKE'%배우%' OR celeb.job_title LIKE'%개그맨%');
use zerobase
select from celeb;
select from snl_show;
select concat('str1','str2', ..);
select concat('concat',' ','test');
select concat('이름: ', name) from celeb;
select column as alias
from tablename;
> select col1, col2, ...
from tablename as alias;select name as '이름' from celeb;
* 예제2
select name as '이름' , agency as '소속사' from celeb;
select concat(name,': ', job_title) as profile from celeb;
select s.season, s.episode, c.name, c.job_title
from celeb as c, snl_show as s
where c.name=s.host;
select concat(s.season,'-', s.episode,'(', s.broadcast_date,')') as '방송정보' ,
concat(c.name,'(',c.job_title,')') as '출연자정보'
from celeb as c, snl_show as s
where c.name=s.host;
select distinct col1, co2, ...
from tablename;
select distinct agency from celeb;
select distinct sex, job_title from celeb where job_title LIKE '%가수%';
select col, co2, ...
from table
where condition
LIMIT number;
select * from celeb LIMIT 3;
select name from celeb order by age LIMIT 4;
select concat('이름:',name, ', ', '소속사:',agency) as '연예인 정보' from celeb
where name LIKE '___';
select celeb.agency as '소속사 정보', concat('나이: ',celeb.age,'(',celeb.sex,')') as '신상정보', concat(snl_show.season,'-',snl_show.episode,',', '방송날짜:',snl_show.broadcast_date) as '출연정보' from celeb, snl_show where celeb.name=snl_show.host AND celeb.agency LIKE '__엔터테이먼트' order by snl_show.broadcast_date desc;