***topic 테이블***
+-----+------------+------------------+-----------+
| tid | title | description | author_id |
+-----+------------+------------------+-----------+
| 1 | HTML | HTML is ... | 1 |
| 2 | CSS | CSS is ... | 2 |
| 3 | JavaScript | JavaScript is .. | 1 |
| 4 | Database | Database is ... | NULL |
+-----+------------+------------------+-----------+
***author 테이블***
+-----+----------+--------+------------+
| aid | name | city | profile_id |
+-----+----------+--------+------------+
| 1 | egoing | seoul | 1 |
| 2 | leezche | jeju | 2 |
| 3 | blackdew | namhae | 3 |
+-----+----------+--------+------------+
***profile 테이블***
+-----+-----------+------------------+
| pid | title | description |
+-----+-----------+------------------+
| 1 | developer | developer is ... |
| 2 | designer | designer is .. |
| 3 | DBA | DBA is ... |
+-----+-----------+------------------+
SELECT * FROM topic LEFT JOIN author
ON topic.author_id = author.aid
이 내용을 해석 해 보면
SELECT * FROM topic LEFT JOIN author
⇒ topic 테이블 왼쪽에 두고 그 오른쪽에 author테이블을 넣겠다.
ON topic.author_id = author.aid
⇒ topic테이블의 author_id(식별자)와 author테이블의 aid(식별자)의 값이 같은데 그걸 참고해서 두개의 테이블을 하나로 만든다.
결과
mysql> SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid;
+-----+------------+------------------+-----------+------+---------+-------+------------+
| tid | title | description | author_id | aid | name | city | profile_id |
+-----+------------+------------------+-----------+------+---------+-------+------------+
| 1 | HTML | HTML is ... | 1 | 1 | egoing | seoul | 1 |
| 2 | CSS | CSS is ... | 2 | 2 | leezche | jeju | 2 |
| 3 | JavaScript | JavaScript is .. | 1 | 1 | egoing | seoul | 1 |
| 4 | Database | Database is ... | NULL | NULL | NULL | NULL | NULL |
+-----+------------+------------------+-----------+------+---------+-------+------------+
SELECT * FROM topic
LEFT JOIN author
ON topic.author_id = author.aid
LEFT JOIN profile
ON author.profile_id = profile.pid;
결과
mysql> SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;
+-----+------------+------------------+-----------+------+---------+-------+------------+------+-----------+------------------+
| tid | title | description | author_id | aid | name | city | profile_id | pid | title | description |
+-----+------------+------------------+-----------+------+---------+-------+------------+------+-----------+------------------+
| 1 | HTML | HTML is ... | 1 | 1 | egoing | seoul | 1 | 1 | developer | developer is ... |
| 2 | CSS | CSS is ... | 2 | 2 | leezche | jeju | 2 | 2 | designer | designer is .. |
| 3 | JavaScript | JavaScript is .. | 1 | 1 | egoing | seoul | 1 | 1 | developer | developer is ... |
| 4 | Database | Database is ... | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+-----+------------+------------------+-----------+------+---------+-------+------------+------+-----------+------------------+
SELECT tid, topic.title, author_id, name, profile.title
AS job_title
FROM topic
LEFT JOIN author
ON topic.author_id = author.aid
LEFT JOIN profile
ON author.profile_id = profile.pid;
SELECT tid, topic.title, author_id, name, profile.title
⇒ 위의 내용들과 비슷하지만 다른점은 표에 이 정보들만 담아 온다.
결과
mysql> SELECT tid, topic.title, author_id, name, profile.title AS job_title FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;
+-----+------------+-----------+---------+-----------+
| tid | title | author_id | name | job_title |
+-----+------------+-----------+---------+-----------+
| 1 | HTML | 1 | egoing | developer |
| 2 | CSS | 2 | leezche | designer |
| 3 | JavaScript | 1 | egoing | developer |
| 4 | Database | NULL | NULL | NULL |
+-----+------------+-----------+---------+-----------+
작성자가 egoing인 사람만 표시해 주고 싶다면?
WHERE aid=1
SELECT tid, topic.title, author_id, name, profile.title AS job_title
FROM topic
LEFT JOIN author
ON topic.author_id = author.aid
LEFT JOIN profile
ON author.profile_id = profile.pid
WHERE aid=1;
결과
+-----+------------+-----------+--------+-----------+
| tid | title | author_id | name | job_title |
+-----+------------+-----------+--------+-----------+
| 1 | HTML | 1 | egoing | developer |
| 3 | JavaScript | 1 | egoing | developer |
+-----+------------+-----------+--------+-----------+
+몇 가지 테스트들
mysql> SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid;
+-----+------------+------------------+-----------+------+---------+-------+------------+
| tid | title | description | author_id | aid | name | city | profile_id |
+-----+------------+------------------+-----------+------+---------+-------+------------+
| 1 | HTML | HTML is ... | 1 | 1 | egoing | seoul | 1 |
| 2 | CSS | CSS is ... | 2 | 2 | leezche | jeju | 2 |
| 3 | JavaScript | JavaScript is .. | 1 | 1 | egoing | seoul | 1 |
| 4 | Database | Database is ... | NULL | NULL | NULL | NULL | NULL |
+-----+------------+------------------+-----------+------+---------+-------+------------+
mysql> SELECT * FROM author LEFT JOIN topic ON author.aid = topic.author_id;
+-----+----------+--------+------------+------+------------+------------------+-----------+
| aid | name | city | profile_id | tid | title | description | author_id |
+-----+----------+--------+------------+------+------------+------------------+-----------+
| 1 | egoing | seoul | 1 | 1 | HTML | HTML is ... | 1 |
| 1 | egoing | seoul | 1 | 3 | JavaScript | JavaScript is .. | 1 |
| 2 | leezche | jeju | 2 | 2 | CSS | CSS is ... | 2 |
| 3 | blackdew | namhae | 3 | NULL | NULL | NULL | NULL |
+-----+----------+--------+------------+------+------------+------------------+-----------+
mysql> SELECT * FROM topic RIGHT JOIN author ON topic.author_id = author.aid;
+------+------------+------------------+-----------+-----+----------+--------+------------+
| tid | title | description | author_id | aid | name | city | profile_id |
+------+------------+------------------+-----------+-----+----------+--------+------------+
| 1 | HTML | HTML is ... | 1 | 1 | egoing | seoul | 1 |
| 3 | JavaScript | JavaScript is .. | 1 | 1 | egoing | seoul | 1 |
| 2 | CSS | CSS is ... | 2 | 2 | leezche | jeju | 2 |
| NULL | NULL | NULL | NULL | 3 | blackdew | namhae | 3 |
+------+------------+------------------+-----------+-----+----------+--------+------------+