SQL 전체 조인

1 개요[ | ]

SQL full join, full outer join
SQL 전체 조인, 전체 외부 조인, 풀 조인

SQL Join - 05b A Full Join B.svg

2 예시 1[ | ]

SELECT * FROM employee
SELECT * FROM department
  • MySQL에서는 FULL OUTER JOIN 기능이 지원되지 않는다.
SELECT *
FROM employee FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
  • 대신, 왼쪽 조인과 오른쪽 조인을 UNION하여 구할 수 있다.
SELECT * FROM employee A LEFT JOIN department B ON A.DepartmentID = B.DepartmentID
UNION
SELECT * FROM employee A RIGHT JOIN department B ON A.DepartmentID = B.DepartmentID

3 예시 2[ | ]

Full-outer-join-operation.png

CREATE TABLE Sales (Date date, CountryID int(11), Units int(11));
INSERT INTO Sales (Date, CountryID, Units) VALUES
	('2020-01-01', 1, 40),
	('2020-01-02', 1, 25),
	('2020-01-03', 3, 30),
	('2020-01-04', 2, 35);

CREATE TABLE Countries (ID int(11), Country varchar(16));
INSERT INTO Countries (ID, Country) VALUES
	(1, 'USA'),
	(2, 'Canada'),
	(3, 'Panama'),
	(4, 'Spain');

SELECT * FROM Sales A LEFT JOIN Countries B ON A.CountryID = B.ID
UNION
SELECT * FROM Sales A RIGHT JOIN Countries B ON A.CountryID = B.ID

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}