Join ( inner , left, right , full outer , self )
* join 이란? 두 개 이상의 테이블을 결합하는 것
** INNER JOIN : 두 테이블의 공통영역을 선택하는 조인 방식
select *
from table A
inner join table B
on tableA.column = tableB.column
where condition;
** LEFT JOIN : 두개의 테이블에서 공통영역을 포함해, 왼쪽 테이블의 다른 데이터를 포함하는 조인방식
** RIGHT JOIN : 두개의 테이블에서 공통영역을 포함해, 오른쪽 테이블의 다른 데이터를 포함하는 조인방식
* left join & right join 은 공통영역을 제외하고 어떤 영역의 데이터를 보여줄 것인지에 대한 차이
select *
from tableA
left join tableB
on tableA.column = tableB.column
where condition;
> 공통 영역은 선택한 필드의 값들이 출력되었으나, null 값에 해당되는 정보들은 left join 영역의 값들이다
** FULL OUTER JOIN : 두 개의 테이블에서 공통영역을 포함하여 양쪽 영역의 모든 값들을 출력하는 방식
select *
from tableA
full outer join tableB
on tableA.col = tableB.col;
< 그러나 mysql 에서는 full outer join을 지원하지 않는다 >
< left join 결과 union right join 결과를 통해서 full outer join을 수행할 수 있다 >
* 아래와 같은 방식
select colA.1, colA.2, colB.1, colB.2
from tableA
left join tableB
on colA.1 = colB.1
union
select colA.1, colA.2, colB.1, colB.2
from tableA
right join tableB
on colA.1 = colB.1;
** Self join ( inner join 과 같은 효과 )
select col1, col2
from tableA, tableB,
where tableA.1 = tableB.1 ;
* 만약 self join 시 , 이용하고자 하는 칼럼명이 두 테이블 간 중복이 없다면, table.column을 명시하지 않고도 시스템이 저절로 구분한다, 하지만 가독성 확보의 목적에서 명시하는 것이 좋다.