Knowledge Map

join 본문

DataBase

join

2016. 6. 21. 10:56

출처

https://opentutorials.org/course/195/1409

http://rapapa.net/?p=311


이 그림 하나로 설명이 가능하다.



생활코딩의 테스트 데이터



DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` tinyint(4NOT NULL,
  `name` char(4NOT NULL,
  `sex` enum('남자','여자'NOT NULL,
  `location_id` tinyint(4NOT NULL,
  `birthday` datetime NOT NULL,
  PRIMARY KEY (`id`)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
 

DROP TABLE IF EXISTS `location`;
CREATE TABLE `location` (
`id`  tinyint UNSIGNED NOT NULL AUTO_INCREMENT ,
`name`  varchar(20NOT NULL ,
`distance`  tinyint UNSIGNED NOT NULL ,
PRIMARY KEY (`id`)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

cs



INSERT INTO `location` VALUES (1'서울'10),(2'청주'200),(3'경주'255),(4'제천'190),
                              (5'대전'200),(6'제주'255),(7'영동'255),(8'광주'255),
                              (9'수원'100);
 
cs



INSERT INTO `student` VALUES (1'이숙경''여자'1'1982-11-16 00:00:00');
INSERT INTO `student` VALUES (2'박재숙''남자'2'1985-10-26 00:00:00');
INSERT INTO `student` VALUES (3'백태호''남자'3'1989-2-10 00:00:00');
INSERT INTO `student` VALUES (4'김경훈''남자'4'1979-11-4 00:00:00');
INSERT INTO `student` VALUES (6'김경진''여자'5'1985-1-1 00:00:00');
INSERT INTO `student` VALUES (7'박경호''남자'6'1981-2-3 00:00:00');
INSERT INTO `student` VALUES (8'김정인''남자'5'1990-10-1 00:00:00');
cs



ㅇ FULL OUTTER JOIN은 mysql 에서 작동하지 않는다.

ㅇ OUTTER JOIN: 매칭되는 행이 없어도 결과를 가져오고 매칭되는 행이 없는 경우 NULL 표시 (LEFT JOIN, RIGHT JOIN이 있다.)

ㅇ INNER JOIN : 조인하는두개의 테이블 모두에 데이터가 존재하는 행에 대해서만 결과를 가져온다.


- LEFT OUTER JOIN == LEFT JOIN ,  RIGHT OUTER JOIN == RIGHT JOIN

- JOIN == INNER JOIN


ㅇ 두 테이블의 교집합
SELECT s.name, s.location_id, l.name AS address, l.distance 
FROM student AS s JOIN location AS l ON s.location_id = l.id;

ㅇ student 집합 출력
SELECT s.name, s.location_id, l.name AS address, l.distance  
FROM student AS s LEFT JOIN location AS l ON s.location_id = l.id;

ㅇ student 집합 중에서 location 집합에 없는것을 출력
select s.name, s.location_id, l.name as address, l.distance 
from student as s left join location as l on s.location_id = l.id where l.id is null;

ㅇ location 집합 중에서 student에 없는 것을 출력
select s.name, s.location_id, l.name as address, l.distance 
from student as s right join location as l on s.location_id = l.id where s.location_id is null;

cs



'DataBase' 카테고리의 다른 글

프로시저 쿼리에서의 1064에러  (0) 2017.01.17
latin1 한글정렬  (0) 2016.07.22
DB 데이타 인코딩 변환  (0) 2016.06.20
DB 설정 값, 문자셋 확인  (0) 2016.06.20
convert() 및 cast()  (0) 2016.06.17
Comments