정원혁 교제 155p 블로깅
조인을 걸때는 테이블이 셋 이상이라고 해서 달라질 것은 없다. 다음과 같은 결과를 얻는다고 해보자.
책명 출판사명 수량
그렇다면 가져올 컬럼이 어떤 테이블에 있는지를 찾아내고, 테이블 간의 관계를 찾아내면 된다.
이를 위해서는 ER-D가 절대적으로 필요하다. 만약 머리가 좋아서 ER-D를 모두 외우고 있다면,
몰라도 조인 문을 작성할 떄는 ERD의 도움이 절실히 필요하다.
ERD를 보면 titles와 publishers는 pub_id로, 수량이 있는 sales와 titles는 title_id로 연결된다.
따라서 다음과 같은 JOIN 문이 나온다.
ex1)
select title, pub_name, qty
from titles JOIN publishersON titles.pub_id = publishers.
pub_id
JOIN sales ON sales.title_id = titles.title_id
ex2)
select title, pub_name, qty
from sales JOIN titlesON sales.title_id = titles.title_id
JOIN publishers ON titles.pub_id = publishers.pub_id
조인을 걸때는 테이블이 셋 이상이라고 해서 달라질 것은 없다. 다음과 같은 결과를 얻는다고 해보자.
책명 출판사명 수량
그렇다면 가져올 컬럼이 어떤 테이블에 있는지를 찾아내고, 테이블 간의 관계를 찾아내면 된다.
이를 위해서는 ER-D가 절대적으로 필요하다. 만약 머리가 좋아서 ER-D를 모두 외우고 있다면,
몰라도 조인 문을 작성할 떄는 ERD의 도움이 절실히 필요하다.
ERD를 보면 titles와 publishers는 pub_id로, 수량이 있는 sales와 titles는 title_id로 연결된다.
따라서 다음과 같은 JOIN 문이 나온다.
ex1)
select title, pub_name, qty
from titles JOIN publishersON titles.pub_id = publishers.
pub_id
JOIN sales ON sales.title_id = titles.title_id
ex2)
select title, pub_name, qty
from sales JOIN titlesON sales.title_id = titles.title_id
JOIN publishers ON titles.pub_id = publishers.pub_id