2013. 7. 29. 17:18

INNER JOIN (교집합)

 

INNER JOIN(NORMAL JOIN)은 여러 테이블의 조건들이 모두 만족했을때만 데이타를 보여주게 되며 한쪽 테이블의 조건이라도 만족하지 않으면 데이타를
보여주지 않습니다.

 

LEFT OUTER JOIN (교집합)

 

LEFT OUTER JOIN(교집합)은 여러 테이블의 조건들이 모두 만족하지 않더라도 데이타를 보여줍니다.

 

※ 조건이 만족하지 않는 테이블의 데이타값은 "NULL" 값으로 출력된다.

 

간단한 조인 예제

 

개요

 

출석 테이블을 만든다 가정하면 normal join 으로는, 한번도 출석하지 않은 학생은 목록에도 보이지 않게 되지만, outer join 으로는 모두 볼 수 있게 됩니다.

 

예제

 

[테이블 생성]

 

drop table book;
create table book
(
code varchar(10),
name varchar(10)
);
insert into book values ('1111','사랑책');
insert into book values ('2222','이별책');
insert into book values ('3333','웃긴책');

 

drop table animal;

create table animal
(
code varchar(10),
name varchar(10)
);
insert into animal values ('1111','사자');
insert into animal values ('2222','호랑이');
insert into animal values ('3333','오리');

 

drop table buy;
create table buy
(
book_code varchar(10),
animal_code varchar(10)
);
insert into buy values ('1111','1111');
insert into buy values ('2222','2222');
insert into buy values ('3333','4444');

 

[LEFT OUTER JOIN 예제]

 

select
 b.name, c.name
from
 buy as a
 left outer join book as b on a.book_code = b.code
 left outer join animal as c on a.animal_code = c.code;

 

※ left outer join 시에는 최대한 중복건이 안나오도록 유일한 컬럼으로 비교해야 한다.

※ left outer join 시에 조건에 만족하지 않는 테이블의 컬럼값은 "NULL" 로 표기된다.

 

[INNER JOIN 방법1]

 

select 
 b.name, c.name
from
 buy as a
 inner join book as b on a.book_code = b.code
 inner join animal as c on a.animal_code = c.code;

 

[INNER JOIN 방법2]

 

select 
 b.name, c.name
from
 buy as a, book as b, animal as c
where
 a.book_code = b.code and a.animal_code = c.code where a.code = '1111';

select 
 b.name, c.name
from
 buy as a, book as b, animal as c where a.book_code = '1111';

 

여러 테이블 컬럼의 모든 경우의 수를 출력한다.

 

select * from book, animal, buy;

select * from book a, animal b, buy c;

 

-- 3개테이블 컬럼의 경우의 수를 모두 출력한다.

-- 모든 경우의 수 = 각테이블의 로우스를 곱한값

 

RDBMS에서 JOIN은 중요한개념입니다.

서로 관련된 정보 중 정보의 관계가 1:N으로 설계하게 되면 서로 분리하여 2개의 테이블로 분리시킵니다. 여러개로 나누면서 서로 연결할수있는 공통 컬럼을 만들어두면 여러모로 좋은점이

많습니다. 그렇다고 table하나로 해결하는것이 효율적이지 않다는것은 아니지만 각각의 장단점이

있습니다.

 

table category

create table category(

num int not null primary key auto_increment,

name varchar(20) not null)

insert into category values(null,'컴퓨터');

insert into category values(null,'가전제품');

insert into category values(null,'통신기기');

 

table s_category

create table s_category(

num int not null primary key auto_increment,

cate_num int not null,

name varchar(20) not null)

insert into s_category values(null,1,'cpu');

insert into s_category values(null,2,'냉장고');

insert into s_category values(null,3,'pcs');

 

category 와 s_category테이블을 통해 어떤 대분류에 속한 소분류가 있는지 알아보자

ex)select * from category ,s_category where category.num=s_category.cate_num;

만약에 where절을 주지 않게 되면 table category와 table s_category를 곱한 값이

나오게 됩니다.

category의 첫번째 레코드를 가져온후 s_category의 첫번째레코드를 가져와 조건이 맞으면 출력

category의 첫번째 레코드를 가져온후 s_category의 두번째레코드를 가져와 조건이 맞으면 출력

category의 첫번째 레코드를 가져온후 s_category의 세번째레코드를 가져와 조건이 맞으면 출력

 

category의 두번째 레코드를 가져온후 s_category의 첫번째레코드를 가져와 조건이 맞으면 출력

category의 두번째 레코드를 가져온후 s_category의 두번째레코드를 가져와 조건이 맞으면 출력

category의 두번째 레코드를 가져온후 s_category의 세번째레코드를 가져와 조건이 맞으면 출력

..........

 

where 조건이 없으므로 위의 순서대로 모두 출력이 되는것이다.

 

또한 컴퓨터 카테고리에 속한 소분류만을 보고싶다면 다음과 같이 조인하면 된다.

ex)select a.num,a.name,b.num,b.name from category a,s_category b

where a.num=b.cate_num and a.num=1;


'DBMS > MySQL' 카테고리의 다른 글

MYSQL 성능 향상 정리  (0) 2013.07.29
초급QUERY  (0) 2013.07.29
4.0 추가기능  (0) 2013.07.29
[MySQL] 자동증가값  (0) 2013.07.29
Mysql insert 와 select 같이 쓰기  (0) 2013.07.29
Posted by 1+1은?