[DB] 8. Join

김주희's avatar
Feb 28, 2025
[DB] 8. Join
수정필요
 
  1. 연관관계
  1. 조인
     

    1. 개념?

     
     
    player_tb pno = pk 1 홍길동 2 임꺽정 3 장보고 4 이두희 --------------------------- 인덱스 목차 (310페이지) - 랜덤 엑세스 1. 인덱스 목차 검색 (B-tree) - 검색된 위치에 데이터가 있음(InnoDB (기본 엔진)) 2. 위치를 엑세스 (direct access) 1. 인덱스 목차 검색 (B-tree) - 참조 포인터 2. 위치를 엑세스 (MyISAM) 비트맵 인덱스
     

    2. 예제

    0. 테이블 구조

    1. user_tb
      1. notion image
    1. feed_tb
      1. notion image
    1. reply_tb
      1. notion image

    1. Inner Join

    -- 1. inner join -- 먼저 load한 tb = left tb = driving tb -- on절에서부터 join 시작 -> on 이전에는 그냥 load select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id;
    notion image
     
    +) emp table과 dept table
    select * from emp e inner join dept d on e.deptno = d.deptno;
    notion image
     

    2. 이너조인 실패 사례

    -- 2. 이너조인 실패 사례 -- 조인 결과(인라인 뷰 취급)를 가지고 다시 조인 -- 댓글이 없는 피드는 조회되지 않는 문제 발생 select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id inner join reply_tb rt on ft.id = rt.feed_id;
    notion image
     

    3. Outer Join

    -- 3. outer join (내가 원하는 테이블에 있는 모든 데이터를 다 뽑으면서 조인하기 위해) -- left tb의 모든 데이터가 다 조회되길 원하는 상황 select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id;
    notion image
     

    4. 화면 데이터 완성 (인라인 뷰)

    -- 4. 화면 데이터 완성하기 (인라인 뷰) select * from ( select ft.title feed_title, ft.photo_url feed_picture, ut.username feed_writer, rt.content reply_content, rt.user_id reply_writer_id from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id ) post left outer join user_tb rut on post.reply_writer_id = rut.id;
    notion image
     

    5. 화면 데이터 완성 (Join)

    -- 5. 화면 진짜 완성 select ft.title feed_title, ft.photo_url feed_picture, ut.username feed_writer, rt.content reply_content, rut.username reply_writer from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id left outer join user_tb rut on rt.user_id = rut.id;
    notion image
     

    6. 셀프 조인

    계층을 찾을 때 사용한다 (같은 테이블끼리 join)
    -- 셀프 조인 select e1.empno '사원번호', e1.ename '사원명', e2.ename '상사명' from emp e1 left outer join emp e2 on e1.mgr = e2.empno;
    notion image
     

    전체 코드

    CREATE TABLE user_tb( id int primary key auto_increment, username varchar(20), password varchar(20), email varchar(100) ); CREATE TABLE feed_tb( id int primary key auto_increment, title varchar(1000), photo_url varchar(100), user_id int ); CREATE TABLE reply_tb( id int primary key auto_increment, content varchar(100), user_id int, feed_id int ); insert into user_tb(username, password, email) values('ssar', '1234', 'ssar@nate.com'); insert into user_tb(username, password, email) values('cos', '1234', 'cos@nate.com'); insert into user_tb(username, password, email) values('love', '1234', 'love@nate.com'); insert into feed_tb(title, photo_url, user_id) values('계곡왔어요', 'http://계곡.com', 1); insert into feed_tb(title, photo_url, user_id) values('바다왔어요', 'http://바다.com', 2); insert into reply_tb(content, user_id, feed_id) values('굿', 2, 1); insert into reply_tb(content, user_id, feed_id) values('별로', 3, 1); -- 1. inner join -- 먼저 load한 tb = left tb = driving tb -- on절에서부터 join 시작 -> on 이전에는 그냥 load select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id; -- pk는 화면상에서 안쓰여도 전달해줘야됨 (ex. 삭제할때 pk로 구분해서 삭제) select * from emp; select * from dept; select * from emp e inner join dept d on e.deptno = d.deptno; -- 2. 이너조인 실패 사례 -- 조인 결과(인라인 뷰 취급)를 가지고 다시 조인 -- 댓글이 없는 피드는 조회되지 않는 문제 발생 select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id inner join reply_tb rt on ft.id = rt.feed_id; -- 3. outer join (내가 원하는 테이블에 있는 모든 데이터를 다 뽑으면서 조인하기 위해) -- left tb의 모든 데이터가 다 조회되길 원하는 상황 select * from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id; -- reply의 username이 안보이는 -- 4. 화면 데이터 완성하기 (인라인 뷰) select * from ( select ft.title feed_title, ft.photo_url feed_picture, ut.username feed_writer, rt.content reply_content, rt.user_id reply_writer_id from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id ) post left outer join user_tb rut on post.reply_writer_id = rut.id; -- 5. 화면 진짜 완성 select ft.title feed_title, ft.photo_url feed_picture, ut.username feed_writer, rt.content reply_content, rut.username reply_writer from feed_tb ft inner join user_tb ut on ft.user_id = ut.id left outer join reply_tb rt on ft.id = rt.feed_id left outer join user_tb rut on rt.user_id = rut.id;
     
    Share article

    More articles

    See more posts

    jay0628