JOIN
ํ ํ
์ด๋ธ๊ณผ ๋ค๋ฅธ ํ
์ด๋ธ์ด ์ธ๋ ํค
๋ก ์ฐ๊ฒฐ์ด ๋์ด ์๋ ๊ฒฝ์ฐ ๋ ํ
์ด๋ธ์ ์ฐ๊ฒฐํ์ฌ, ๋ ํ
์ด๋ธ์์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ SELECT
๊ฐ ๊ฐ๋ฅํ๋ค.
INNER JOIN
๋ ํ ์ด๋ธ์ ๋น๊ตํ์ฌ ์กฐ๊ฑด์ด ์ฐธ์ธ COLUMN์ SELECT
๋ ํ ์ด๋ธ ๊ฐ์ ๊ฐ์ด ์ผ์นํ๋ ํ๋ง ํฌํจ ( ๊ต์งํฉ )
SELECT column_list
FROM table_1
INNER JOIN table_2 ON join_condition;
join_condition์ด ์ฐธ์ธ COLUMN์ SELECTํ๋ ๊ฒฝ์ฐ์ด๋ค.
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
INNER JOIN committees c
ON c.name = m.name;
//INNER JOIN committees c
// USING(name);
๋ ํ
์ด๋ธ์ colmun_name์ด ๊ฐ์ table_1.name = table_2.name
์ธ ๊ฒฝ์ฐ USING
์ ์ฌ์ฉํ์ฌ ํํ ํ ์ ์๋ค.
LEFT JOIN
์ผ์ชฝ ํ ์ด๋ธ์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ๊ฐ์ ๊ฐ์ ธ์ค๋, ์ค๋ฅธ์ชฝ๊ณผ ๊ฐ์ ๊ฐ์ ๊ฐ์ ์ฑ์์ ๊ฐ์ ธ์ค๊ณ ์ค๋ฅธ์ชฝ๊ณผ ๊ฐ์ง ์์ ๊ฐ์ NULL๋ก selectํ๋ค
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
LEFT JOIN committees c USING(name);
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
LEFT JOIN committees c USING(name)
WHERE c.committee_id IS NULL;
where์ is null
์ ์ถ๊ฐํ์ฌ ์ผ์ชฝํ
์ด๋ธ์์ ๊ต์งํฉ์ ๋บ ๊ฐ์ selectํ ์ ์๋ค.
RIGHT JOIN
left join์ ๋ฐ๋๋ก ์ค๋ฅธ์ชฝ ํ ์ด๋ธ์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ๊ฐ์ ๊ฐ์ ธ์ค๋, ์กฐ๊ฑด์ด ์ฐธ์ด ์๋ ๊ฐ์ null๋ก ๊ฐ์ ธ์จ๋ค.
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
RIGHT JOIN committees c on c.name = m.name;
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
RIGHT JOIN committees c USING(name)
WHERE c.committee_id IS NULL;
where์ is null
์ ์ถ๊ฐํ์ฌ ์ค๋ฅธ์ชฝํ
์ด๋ธ์์ ๊ต์งํฉ์ ๋บ ๊ฐ์ selectํ ์ ์๋ค.
JOIN ON์์ ์กฐ๊ฑด ์ถ๊ฐ์ WHERE ์กฐ๊ฑด ๋น๊ต
SELECT
o.orderNumber,
customerNumber,
productCode
FROM
orders o
LEFT JOIN orderDetails
USING (orderNumber)
WHERE
orderNumber = 10123;
SELECT
o.orderNumber,
customerNumber,
productCode
FROM
orders o
LEFT JOIN orderDetails d
ON o.orderNumber = d.orderNumber AND
o.orderNumber = 10123;
์์ ๋ ์ฟผ๋ฆฌ ๋ฌธ์ ๋น๊ตํด๋ณด๋ฉด, ์ฒซ๋ฒ์งธ ์ฟผ๋ฆฌ๋ฌธ์ ordernumber์ด ๊ฐ์ column๋ง selectํ where์ ๋ก ์ธํด ordernumber์ด 10123์ธ column๋ง selectํ๋ค.
๋๋ฒ์งธ ์ฟผ๋ฆฌ๋ฌธ์, ๋ชจ๋ ordernumber์ด ๊ฐ์ column์ ๋ํด ordernumber์ด 10123์ธ column๋ง ๊ฐ์ด ๋ค์ด์๊ณ ๋ค๋ฅธ ๋ชจ๋ column์ null๊ฐ์ด ๋ค์ด์๋ ํ ์ด๋ธ์ ๋ชจ๋ selectํ๋ ์ฟผ๋ฆฌ๋ฌธ์ด๋ค.
CROSS JOIN
ํฌ๋ก์ค ์กฐ์ธ์ on ์กฐ๊ฑด ์๊ณ , ๋ชจ๋ ํ ์ด๋ธ์ ํ์ ๊ฒฐํฉํ๋ค. (ํ๋ ฌ์ ๊ณฑ, mxn)
SELECT
m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM
members m
CROSS JOIN committees c;
SELF JOIN
ํ ํ ์ด๋ธ์์ ์๊ธฐ ์์ ์ ์ฐธ์กฐํ์ฌ ํ column๊ณผ ๋ค๋ฅธ column์ ๋น๊ตํ ๋ ์ฌ์ฉํ๋ค.
SELF JOIN๋ INNER JOIN๊ณผ LEFT JOIN์ ์ด์ฉํ์ฌ ๊ต์งํฉ๋ง ํ์ํ ์ง, NULL๊ฐ์ ํฌํจํ ๊ฐ์ ๊ฐ์ ธ ์ฌ์ง ์ํฉ์๋ฐ๋ผ ์ ํํ์ฌ ์ฌ์ฉํ๋ฉด๋๋ค.
INNER JOIN
SELECT CONCAT(m.lastName, ', ', m.firstName) AS 'Manager', CONCAT(e.lastName, ', ', e.firstName) AS 'Direct report' FROM employees e INNER JOIN employees m ON m.employeeNumber = e.reportsTo ORDER BY Manager;
LEFT JOIN
SELECT CONCAT(m.lastname, ', ', m.firstname) AS 'Manager', CONCAT(e.lastname, ', ', e.firstname) AS 'Direct report' FROM employees e LEFT JOIN employees m ON m.employeeNumber = e.reportsto ORDER BY manager DESC;
Last updated