use zerobase;
Database changed
mysql> select * from celeb;
+----+--------+------------+------+------+------------------+------------------+
| ID | NAME | BIRTHDAY | AGE | SEX | JOB_TITLE | AGENCY |
+----+--------+------------+------+------+------------------+------------------+
| 1 | 아이유 | 1993-05-16 | 29 | F | 가수, 탤런트 | EDAM엔터테인먼트 |
| 2 | 이미주 | 1994-09-23 | 28 | F | 가수 | 울림엔터테인먼트 |
| 3 | 송강 | 1994-04-23 | 28 | M | 탤런트 | 나무엑터스 |
| 4 | 강동원 | 1981-01-18 | 41 | M | 영화배우, 탤런트 | YG엔터테인먼트 |
| 5 | 유재석 | 1972-08-14 | 50 | M | MC, 개그맨 | 안테나 |
| 6 | 차승원 | 1970-06-07 | 48 | M | 영화배우, 모델 | YG엔터테인먼트 |
| 7 | 이수현 | 1999-05-04 | 23 | F | 가수 | YG엔터테인먼트 |
+----+--------+------------+------+------+------------------+------------------+
mysql> 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
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO snl_show values (1, 8, 7, '2020-09-05', '강동원');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO snl_show values (2, 8, 8, '2020-09-12', '유재석');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (3, 8, 9, '2020-09-19', '차승원');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (4, 8, 10, '2020-09-26', '이수현');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (5, 9, 1, '2021-09-04', '이병헌');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (6, 9, 2, '2021-09-11', '하지원');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (7, 9, 3, '2021-09-18', '제시');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (8, 9, 4, '2021-09-25', '조정석');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (9, 9, 5, '2021-10-02', '조여정');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO snl_show values (10, 9, 6, '2021-10-09', '옥주현');
Query OK, 1 row affected (0.00 sec)
mysql> select * from snl_show;
+----+--------+---------+----------------+--------+
| ID | SEASON | EPISODE | BROADCAST_DATE | HOST |
+----+--------+---------+----------------+--------+
| 1 | 8 | 7 | 2020-09-05 | 강동원 |
| 2 | 8 | 8 | 2020-09-12 | 유재석 |
| 3 | 8 | 9 | 2020-09-19 | 차승원 |
| 4 | 8 | 10 | 2020-09-26 | 이수현 |
| 5 | 9 | 1 | 2021-09-04 | 이병헌 |
| 6 | 9 | 2 | 2021-09-11 | 하지원 |
| 7 | 9 | 3 | 2021-09-18 | 제시 |
| 8 | 9 | 4 | 2021-09-25 | 조정석 |
| 9 | 9 | 5 | 2021-10-02 | 조여정 |
| 10 | 9 | 6 | 2021-10-09 | 옥주현 |
+----+--------+---------+----------------+--------+
SELECT column1, column2, ...
FROM tableA
INNER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
mysql> select celeb.id, celeb.name, snl_show.id, snl_show.host
-> from celeb
-> inner join snl_show
-> on celeb.name = snl_show.host;
+----+--------+----+--------+
| id | name | id | host |
+----+--------+----+--------+
| 4 | 강동원 | 1 | 강동원 |
| 5 | 유재석 | 2 | 유재석 |
| 6 | 차승원 | 3 | 차승원 |
| 7 | 이수현 | 4 | 이수현 |
+----+--------+----+--------+
SELECT column1, column2, ...
FROM tableA
LEFT JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
mysql> select celeb.id, celeb.name, snl_show.id, snl_show.host
-> from celeb
-> left join snl_show
-> on celeb.name = snl_show.host;
+----+--------+------+--------+
| id | name | id | host |
+----+--------+------+--------+
| 1 | 아이유 | NULL | NULL |
| 2 | 이미주 | NULL | NULL |
| 3 | 송강 | NULL | NULL |
| 4 | 강동원 | 1 | 강동원 |
| 5 | 유재석 | 2 | 유재석 |
| 6 | 차승원 | 3 | 차승원 |
| 7 | 이수현 | 4 | 이수현 |
+----+--------+------+--------+
SELECT column1, column2, ...
FROM tableA
RIGHT JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
mysql> select celeb.id, celeb.name, snl_show.id, snl_show.host
-> from celeb
-> right join snl_show
-> on celeb.name = snl_show.host;
+------+--------+----+--------+
| id | name | id | host |
+------+--------+----+--------+
| 4 | 강동원 | 1 | 강동원 |
| 5 | 유재석 | 2 | 유재석 |
| 6 | 차승원 | 3 | 차승원 |
| 7 | 이수현 | 4 | 이수현 |
| NULL | NULL | 5 | 이병헌 |
| NULL | NULL | 6 | 하지원 |
| NULL | NULL | 7 | 제시 |
| NULL | NULL | 8 | 조정석 |
| NULL | NULL | 9 | 조여정 |
| NULL | NULL | 10 | 옥주현 |
+------+--------+----+--------+
SELECT column1, column2, ...
FROM tableA
FULL OUTER JOIN tableB
ON tableA.column = tableB.column
WHERE condition;
mysql> select celeb.id, celeb.name, snl_show.id, snl_show.host
-> from celeb
-> full outer join snl_show
-> on celeb.name = snl_show.host;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join snl_show
on celeb.name = snl_show.host' at line 3
🔺 MySQL에서는 FULL JOIN을 지원하지 않으므로 다음의 쿼리로 같은 결과를 만들 수 있다.
SELECT column1, column2, ...
FROM tableA
LEFT JOIN tableB on tableA.column = tableB.column
UNION
SELECT column1, column2, ...
FROM tableA
RIGHT JOIN tableB ON tableA.column = tableB.column
WHERE condition;
mysql> 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;
+------+--------+------+--------+
| id | name | id | host |
+------+--------+------+--------+
| 1 | 아이유 | NULL | NULL |
| 2 | 이미주 | NULL | NULL |
| 3 | 송강 | NULL | NULL |
| 4 | 강동원 | 1 | 강동원 |
| 5 | 유재석 | 2 | 유재석 |
| 6 | 차승원 | 3 | 차승원 |
| 7 | 이수현 | 4 | 이수현 |
| NULL | NULL | 5 | 이병헌 |
| NULL | NULL | 6 | 하지원 |
| NULL | NULL | 7 | 제시 |
| NULL | NULL | 8 | 조정석 |
| NULL | NULL | 9 | 조여정 |
| NULL | NULL | 10 | 옥주현 |
+------+--------+------+--------+
SELECT column1, column2, ...
FROM tableA, tableB, ...
WHERE condition;
mysql> select celeb.id, celeb.name, snl_show.id, snl_show.host
-> from celeb, snl_show
-> where celeb.name = snl_show.host;
+----+--------+----+--------+
| id | name | id | host |
+----+--------+----+--------+
| 4 | 강동원 | 1 | 강동원 |
| 5 | 유재석 | 2 | 유재석 |
| 6 | 차승원 | 3 | 차승원 |
| 7 | 이수현 | 4 | 이수현 |
+----+--------+----+--------+
mysql> select celeb.name, celeb.job_title, snl_show.host
-> from celeb, snl_show
-> where celeb.name = snl_show.host and celeb.agency = '안테나';
+--------+------------+--------+
| name | job_title | host |
+--------+------------+--------+
| 유재석 | MC, 개그맨 | 유재석 |
+--------+------------+--------+
mysql> select celeb.name, celeb.age, celeb.job_title, celeb.agency, season, episode
-> from celeb, snl_show
-> where name=host and ((not job_title like '%영화배우%' and agency='yg엔터테인먼트') or (age>=40 and not agency like '%YG엔터테인먼트%'));
+--------+------+------------+----------------+--------+---------+
| name | age | job_title | agency | season | episode |
+--------+------+------------+----------------+--------+---------+
| 유재석 | 50 | MC, 개그맨 | 안테나 | 8 | 8 |
| 이수현 | 23 | 가수 | YG엔터테인먼트 | 8 | 10 |
+--------+------+------------+----------------+--------+---------+
mysql> select snl_show.id, season, episode, name, job_title
-> from celeb, snl_show
-> where name=host;
+----+--------+---------+--------+------------------+
| id | season | episode | name | job_title |
+----+--------+---------+--------+------------------+
| 1 | 8 | 7 | 강동원 | 영화배우, 탤런트 |
| 2 | 8 | 8 | 유재석 | MC, 개그맨 |
| 3 | 8 | 9 | 차승원 | 영화배우, 모델 |
| 4 | 8 | 10 | 이수현 | 가수 |
+----+--------+---------+--------+------------------+
mysql> select name, season, episode, broadcast_date, agency
-> from celeb, snl_show
-> where name=host and ((episode in (7, 9, 10) or (agency like 'yg______')) and broadcast_date > '2020-09-15');
+--------+--------+---------+----------------+----------------+
| name | season | episode | broadcast_date | agency |
+--------+--------+---------+----------------+----------------+
| 차승원 | 8 | 9 | 2020-09-19 | YG엔터테인먼트 |
| 이수현 | 8 | 10 | 2020-09-26 | YG엔터테인먼트 |
+--------+--------+---------+----------------+----------------+