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
);
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, '2021-09-04', '이병현');
INSERT INTO snl_show VALUES (6, 9, 2, '2021-09-11', '하지원');
INSERT INTO snl_show VALUES (7, 9, 3, '2021-09-18', '제시');
INSERT INTO snl_show VALUES (8, 9, 4, '2021-09-25', '조정석');
INSERT INTO snl_show VALUES (9, 9, 5, '2021-10-02', '조여정');
INSERT INTO snl_show VALUES (10, 9, 6, '2021-10-09', '옥주현');
JOIN 종류
SELECT column1, column2, ...
FROM tableA
INNER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
※ 테이블을 2개 이상 사용하는 경우, column명 앞에 테이블을 작성해 구분할 수 있게 작성
table명.column명
SELECT celeb.id, celeb.name, snl_show.id, snl_show.host
FROM celeb
INNER JOIN snl_show
ON celeb.name = snl_show.host;
SELECT column1, column2, ...
FROM tableA
LEFT 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;
SELECT column1, column2, ...
FROM tableA
RIGHT JOIN tableB
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;
SELECT column1, column2, ...
FROM tableA
FULL OUTER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
SELECT celeb.id, celeb.name, snl_show.id, snl_show.host
FROM celeb
FULL OUTER JOIN snl_show
ON celeb.name = snl_show.host;
MySQL에서는 FULL JOIN을 지원하지 않음
- 아래 쿼리로 같은 결과를 만들 수 있음
SELECT column1, column2, ...
FROM tableA
LEFT JOIN tableB ON .column .column
UNION
SELECT column1, column2, ...
FROM
RIGHT JOIN .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;
SELECT column1, column2, ...
FROM tableA, tableB, ...
WHERE condition;
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엔터테이먼트'));
동일한 칼럼명이 존재하는 경우에만 테이블명 암시
Ex) snl_show에 출연한 celeb 중, 에피소드 7, 9, 10 중에 출연했거나 소속사가 YG로 시작하고 뒤에 6글자로 끝나는 사람 중 2020년 9월 15일 이후에 출연했던 사람을 검색
SELECT celeb.name, snl_show.season, snl_show.broadcast_date, snl_show.episode, celeb.agency
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');
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 '%개그맨%'));