본문 바로가기

SQL/SQL Developer

JOIN - LEFT OUTER JOIN(LEFT JOIN) 실습OIN - LEFT OUTER JOIN(LEFT JOIN) 실습

https://sql-joins.leopard.in.ua/

 

 

SQL Joins Visualizer

Please select how do you want to do SQL JOIN between two table

sql-joins.leopard.in.ua

(벤 다이어그램을 통해ql-joins.leopard.in.ua/

 

 

 

SQL Joins Visualizer

 

Please select how do you want to do SQL JOIN between two table

 

sql-joins.leopard.in.ua

(벤 다이어그램을 통해 쉽게 이해하기 쉬운 사이트)

 


총 3개의 테이블


[topic 테이블을 보여주는 명령어]

SELECT * FROM topic;

 

topic이라는 표를 보여줄 것이다.


[topic 테이블을 왼쪽에 놓고 author 테이블의  author_id에  해당하는 번호에 들어가 있는 정보 표를 결합하는 명령어] 

SELECT * FROM topic LEFT JOIN author ON topic.author_id = author_aid;

tid 4인 경우 해당 author_id에 대한 정보가 없기 때문에 NULL인 상태를 보여준다.


이번엔 무엇이냐?

topic 테이블에 또 LEFT JOIN을 해주는 것이다.

오른쪽에다가 profile이라는 테이블을.

누구와?

author.profile_id 와 profile.pid 값이 같다라고 알려주는 것이다.

 

SELECT * FROM topic 
     LEFT JOIN author 
          ON topic.author_id = author.aid 
     LEFT JOIN profile 
          ON author.profile_id = profile.pid
          ;

profile행이 결합하여 만들어진 표


[ tid 값, topic의 title, athor_id 값, name, profile의 title은 별칭을 주고 싶다 -> AS jod_tile까지 출력하는 명령]

 

AS = Alias (별명, 별칭)

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;

 

원하는 정보만 결합된 테이블


[author_id 값이 1번인  즉 egoing이라는 사람이 작성한 글만을 행으로만 표현하고 싶다. => WHERE aid = 1;]

SELECT tid, topic.title, author_id, name, profile.titles 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;

egoing이라는 사람이 작성한 글들만 가져와  결합된 테이블

 

 

 

'SQL > SQL Developer' 카테고리의 다른 글

FULL OUTER JOIN  (0) 2021.04.05
INNER JOIN  (0) 2021.04.05
테이블 분해/ 조립 - 조립 실행하기(JOIN)  (0) 2021.04.03
테이블 분해/조립 - 분해 실행하기  (0) 2021.04.03
SQL Developer  (0) 2021.04.03