Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[55] 230317 SQL Ch. 7 ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ” ๋‹ค๋ฃจ๊ธฐ: 31๊ฐ• ์ง‘ํ•ฉ ์—ฐ์‚ฐ, 32๊ฐ• ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ, 33๊ฐ• ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[55] 230317 SQL Ch. 7 ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ” ๋‹ค๋ฃจ๊ธฐ: 31๊ฐ• ์ง‘ํ•ฉ ์—ฐ์‚ฐ, 32๊ฐ• ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ, 33๊ฐ• ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54์ผ]

yjyuwisely 2023. 3. 16. 16:36

230317 Fri 55th class

Ch 7. ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ” ๋‹ค๋ฃจ๊ธฐ

์ง„๋„: p.  274 ~ (๊ต์žฌ: SQL ์ฒซ๊ฑธ์Œ, ์ €์ž: ์•„์‚ฌ์ด ์•„์ธ ์‹œ)
MySQL Workbench Manual: https://dev.mysql.com/doc/workbench/en/
MySQL ํŠœํ† ๋ฆฌ์–ผ: https://www.tutorialspoint.com/mysql/

SQLD ์š”์•ฝ pdf: https://yurimac.tistory.com/40
์ฑ… ์š”์•ฝ (9p) pdf: https://brunch.co.kr/@ydy702/10

 

SQL ์ฒซ๊ฑธ์Œ | ์•„์‚ฌ์ด ์•„์ธ ์‹œ - ๊ต๋ณด๋ฌธ๊ณ 

SQL ์ฒซ๊ฑธ์Œ | ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ์ •๋ง ์‰ฌ์šด SQL ์ž…๋ฌธ์„œ๋ฅผ ๋งŒ๋‚˜๋ณด์ž!๋ณธ๊ฒฉ์ ์ธ ๋น…๋ฐ์ดํ„ฐ ์‹œ๋Œ€์— ์ ‘์–ด๋“ค๋ฉด์„œ ๊ธฐ์กด ๋Œ€๋น„ ๋” ํšจ์œจ์ ์ด๊ณ  ํญ๋„“์€ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ์ˆ˜์š”๊ฐ€ ์ปค์งˆ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋˜๋Š” ๊ฐ€์šด๋ฐ, SQ

product.kyobobook.co.kr

๊ณต๋ถ€ํ•œ ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


์š”์•ฝ
FROM JOIN ์ ˆ ํ˜•ํƒœ

FROM JOIN 
์ ˆ ํ˜•ํƒœ
์„ค๋ช…

๊ต์ฐจ ๊ฒฐํ•ฉ
CROSS JOIN

์นดํ‹ฐ์‹œ์•ˆ ๊ณฑ
์–‘์ชฝ ์ง‘ํ•ฉ์˜ M*N๊ฑด์˜ ๋ฐ์ดํ„ฐ ์กฐํ•ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค.




๋‚ด๋ถ€ ๊ฒฐํ•ฉ
INNER JOIN




JOIN ์กฐ๊ฑด์—์„œ ๋™์ผํ•œ ๊ฐ’์ด ์žˆ๋Š” ํ–‰๋งŒ ๋ฐ˜ํ™˜, USING์ด๋‚˜ ON ์ ˆ์„ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ

-- ๋‚ด๋ถ€๊ฒฐํ•ฉ(inner joinํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉ, ์ด๋•Œ inner๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Œ.)
select ์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜

-- from ์ƒํ’ˆ inner join ์žฌ๊ณ ์ˆ˜
from ์ƒํ’ˆ join ์žฌ๊ณ ์ˆ˜
on ์ƒํ’ˆ.์ƒํ’ˆ์ฝ”๋“œ ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ; 










์™ธ๋ถ€ ๊ฒฐํ•ฉ
OUTER JOIN 









JOIN ์กฐ๊ฑด์—์„œ ๋™์ผํ•œ ๊ฐ’์ด ์—†๋Š” ํ–‰๋„ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค, USING ์ด๋‚˜ ON ์กฐ๊ฑด์ ˆ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ. SQL ์‹์—์„œ (+) ์•ˆ ๋ถ™์€ ์ชฝ์œผ๋กœ JOINํ•œ๋‹ค.
LEFT OUTER JOIN: A์—๋งŒ ์žˆ๊ณ  B๋Š” NULL ์ธ ๊ฐ’ ํฌํ•จ

๋จผ์ € ํ‘œ๊ธฐ๋œ ์ขŒ์ธก ํ…Œ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์€ ํ›„ ๋‚˜์ค‘ ํ‘œ๊ธฐ๋œ ์šฐ์ธก ํ…Œ์ด๋ธ”์—์„œ JOIN ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์˜จ๋‹ค. ์šฐ์ธก ๊ฐ’์—์„œ ๊ฐ™์€ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ NULL ๊ฐ’์œผ๋กœ ์ฑ„์šด๋‹ค
RIGHT OUTER JOIN: A์— ์—†๊ณ  B์—๋งŒ ์žˆ๋Š” ๊ฐ’ ํฌํ•จ

LEFT OUTER JOIN์˜ ๋ฐ˜๋Œ€
FULL OUTER JOIN: ์ขŒ์šฐ์ธก ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด JOINํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋Š” ์‚ญ์ œํ•œ๋‹ค.


NATURAL JOIN


๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ–๋Š” ๋ชจ๋“  ์นผ๋Ÿผ๋“ค์— ๋Œ€ํ•ด EQUI JOIN(๋“ฑ๊ฐ€ ์กฐ์ธ)์ˆ˜ํ–‰, 
NATURAL JOIN์ด ๋ช…์‹œ๋˜๋ฉด ์ถ”๊ฐ€๋กœ USING, ON,  WHERE ์ ˆ์—์„œJOIN ์กฐ๊ฑด์„ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค, SQL Sever๋Š” X

USING ์กฐ๊ฑด์ ˆ

๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์นผ๋Ÿผ๋“ค ์ค‘์—์„œ ์›ํ•˜๋Š” ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ๋งŒ ์„ ํƒ์ ์œผ๋กœ EQUI JOIN(๋“ฑ๊ฐ€ ์กฐ์ธ)์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. JOIN ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ ALIAS๋‚˜ ํ…Œ์ด๋ธ” ์ด๋ฆ„๊ณผ ๊ฐ™์€ ์ ‘๋‘์‚ฌ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์—†๋‹ค , SQL Server ์ง€์›x

ON ์กฐ๊ฑด์ ˆ

ON ์กฐ๊ฑด์ ˆ๊ณผ WHERE ์กฐ๊ฑด์ ˆ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ดํ•ด๊ฐ€ ์‰ฌ์šฐ๋ฉฐ ์นผ๋Ÿผ๋ช…์ด ๋‹ค๋ฅด๋”๋ผ๋„ JOIN ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ALIAS๋‚˜ ํ…Œ์ด๋ธ”๋ช… ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉ

์ฐธ๊ณ : https://keep-cool.tistory.com/41


Ch. 7 ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ” ๋‹ค๋ฃจ๊ธฐ

์ฑ…์˜ ๋ชฉ์ฐจ
__31๊ฐ•__์ง‘ํ•ฉ ์—ฐ์‚ฐ
1. SQL๊ณผ ์ง‘ํ•ฉ
2. UNION์œผ๋กœ ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
3. ๊ต์ง‘ํ•ฉ๊ณผ ์ฐจ์ง‘ํ•ฉ

__32๊ฐ•__ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ
1. ๊ณฑ์ง‘ํ•ฉ๊ณผ ๊ต์ฐจ๊ฒฐํ•ฉ
2. ๋‚ด๋ถ€๊ฒฐํ•ฉ
3. INNER JOIN์œผ๋กœ ๋‚ด๋ถ€ ๊ฒฐํ•ฉํ•˜๊ธฐ
4. ๋‚ด๋ถ€๊ฒฐํ•ฉ์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
5. ์™ธ๋ถ€๊ฒฐํ•ฉ

__33๊ฐ•__๊ด€๊ณ„ํ˜• ๋ชจ๋ธ
1. ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ
2. ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ๊ณผ SQL 

31๊ฐ• ์ง‘ํ•ฉ ์—ฐ์‚ฐ p. 274

'๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ”'์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณธ๋‹ค.
๊ด€๊ณ„ํ˜• ๋ชจ๋ธ์„ ์ฑ„ํƒํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๊ณ  ํ•œ๋‹ค. 


31.1 SQL๊ณผ ์ง‘ํ•ฉ p. 275

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋Š” ํ…Œ์ด๋ธ”์˜ ํ–‰์ด ์š”์†Œ์— ํ•ด๋‹นํ•œ๋‹ค. 
ํ•˜๋‚˜์˜ ํ–‰์ด ๊ณง ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ ๋œ๋‹ค.
SELECT ๋ช…๋ น์œผ๋กœ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ ์ „์ฒด๋ฅผ ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. 


31.2 UNION์œผ๋กœ ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

- UNION
ํ•ฉ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•  ๊ฒฝ์šฐ์—๋Š” UNION ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

์˜ˆ์ œ 7-1) UNION์œผ๋กœ ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

SELECT * FROM sample71_a;

SELECT * FROM sample71_b;

์˜ˆ์ œ 7-2) ๋‘ ๊ฐœ์˜ SELECT ๋ช…๋ น์„ UNION์œผ๋กœ ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. 

SELECT * FROM sample71_a
UNION -- ๋‘ ๊ฐœ์˜ ๋ช…๋ น์„ ํ•˜๋‚˜์˜ ๋ช…๋ น์œผ๋กœ ํ•ฉ์นœ๋‹ค.
SELECT * FROM sample71_b;

Point: UNION์œผ๋กœ ๋‘ ๊ฐœ์˜ SELECT ๋ช…๋ น์„ ํ•˜๋‚˜๋กœ ์—ฐ๊ณ„ํ•ด ์งˆ์˜(Query) ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.


์™„์ „ํžˆ ์—ด ๊ตฌ์„ฑ์ด ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์„ UNION์œผ๋กœ ๋ฌถ์„ ์ˆ˜๋Š” ์—†๋‹ค.
(๊ฐ™์€ ์นธ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค. ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งž์•„์•ผ ํ•œ๋‹ค.
์—ด์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์—ฌ ๊ฐ SELECT ๋ช…๋ น์—์„œ ์ง‘ํ•ฉ์˜ ์š”์†Œ๊ฐ€ ๋  ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋กœ ๋งž์ถฐ์ฃผ๋ฉด UNION์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ฟผ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

ํ…Œ์ด๋ธ” sample31

SELECT a FROM sample71_a
UNION
SELECT b FROM sample71_b
UNION
SELECT age FROM sample31;

ํ…Œ์ด๋ธ” sample31์˜ age
ํ…Œ์ด๋ธ” sample31์˜ name

UNION์œผ๋กœ ๋ฌถ์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ ๋ชจ๋‘ ๊ฐ™์ง€๋งŒ ๊ฒฐ๊ด๊ฐ’์˜ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ฌ๋ผ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. 

SELECT * FROM sample71_a UNION SELECT * FROM sample71_b;
SELECT * FROM sample71_b UNION SELECT * FROM sample71_a;

๊ฒฐ๊ด๊ฐ’์˜ ๋‚˜์—ด ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋‹ค.


1) UNION์„ ์‚ฌ์šฉํ•  ๋•Œ์˜ ORDER BY
ํ•ฉ์ง‘ํ•ฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•˜๋ฏ€๋กœ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์˜ SELECT ๋ช…๋ น์— ORDER BY๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. 

-- ๋งˆ์ง€๋ง‰์˜ SELECT ๋ช…๋ น์— ORDER BY๋ฅผ ์ง€์ •ํ•œ๋‹ค.
SELECT a AS c FROM sample71_a
UNION
SELECT b AS c FROM sample71_b ORDER BY c;

๋ณ„๋ช…์œผ๋กœ ์—ด ์ด๋ฆ„์„ ์„œ๋กœ ์ผ์น˜์‹œํ‚จ๋‹ค.

๋‘ ๊ฐœ์˜ SELECT ๋ช…๋ น์—์„œ ์—ด ์ด๋ฆ„์ด ์„œ๋กœ ์ผ์น˜ํ•ด์•ผ ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์„œ๋กœ ๋™์ผํ•˜๊ฒŒ ๋ณ„๋ช…์„ ๋ถ™์—ฌ์„œ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Point: UNION์œผ๋กœ SELECT ๋ช…๋ น์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ SELECT ๋ช…๋ น์— ๋Œ€ํ•ด์„œ๋งŒ ORDER BY ๊ตฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 
ORDER BY ๊ตฌ์— ์ง€์ •ํ•˜๋Š” ์—ด์€ ๋ณ„๋ช…์„ ๋ถ™์—ฌ ์ด๋ฆ„์„ ์ผ์น˜์‹œํ‚จ๋‹ค. 


2) UNION ALL

์˜ˆ์ œ 7-3) ๋‘ ๊ฐœ์˜ SELECT ๋ช…๋ น์— UNION ALL์„ ์ ์šฉํ•ด ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋„ ํฌํ•จํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. 

SELECT * FROM sample71_a
UNION ALL
SELECT * FROM sample71_b;


31.3 ๊ต์ง‘ํ•ฉ(INTERSECT)๊ณผ ์ฐจ์ง‘ํ•ฉ(EXCEPT) p. 282

MySQL์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๊ต์ง‘ํ•ฉ(๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„): INTERSECT
์ฐจ์ง‘ํ•ฉ(์ง‘ํ•ฉ์—์„œ ๋‹ค๋ฅธ ์ชฝ์˜ ์ง‘ํ•ฉ์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‚จ์€ ๋ถ€๋ถ„): EXCEPT (Oracle: MiNUS)
๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๊ณต์ง‘ํ•ฉ(์›์†Œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ์ง‘ํ•ฉ): ์™„์ „ํžˆ ๊ฐ™์€ ์ง‘ํ•ฉ๋ผ๋ฆฌ ์ฐจ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋ฉด ์•„๋ฌด๋Ÿฐ ์š”์†Œ๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.


32๊ฐ• ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ (join) p. 284

RDBMS(Relational database management system)์—์„œ ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด๋‹ค. 


32.1 ๊ณฑ์ง‘ํ•ฉ๊ณผ ๊ต์ฐจ๊ฒฐํ•ฉ

๊ณฑ์ง‘ํ•ฉ์€ ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์„ ๊ณฑํ•˜๋Š” ์—ฐ์‚ฐ ๋ฐฉ๋ฒ•์œผ๋กœ '์ ์ง‘ํ•ฉ' ๋˜๋Š” '์นดํ‹ฐ์ „๊ณฑ(Cartesian product)'์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค. 

1) ๊ต์ฐจ๊ฒฐํ•ฉ(Cross Join)

๊ต์ฐจ ๊ฒฐํ•ฉ

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2

์˜ˆ์ œ 7-4) sample72_x์™€ sample72_y

SELECT * FROM sample72_x;
SELECT * FROM sample72_y;

์˜ˆ์ œ 7-5) FROM ๊ตฌ๋กœ ๊ณฑ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ 

-- FROM๊ตฌ์— ํ…Œ์ด๋ธ” ๋‘ ๊ฐœ๋ฅผ ์ง€์ •ํ•ด ๊ณฑ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ
SELECT * FROM sample72_x, sample72_y;

Point: FROM ๊ตฌ์— ๋ณต์ˆ˜์˜ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•˜๋ฉด ๊ต์ฐจ๊ฒฐํ•ฉ์„ ํ•œ๋‹ค.


2) UNION ์—ฐ๊ฒฐ๊ณผ ๊ฒฐํ•ฉ ์—ฐ๊ฒฐ์˜ ์ฐจ์ด
Point: ๊ฒฐํ•ฉ์€ ์—ด(๊ฐ€๋กœ =>)๋ฐฉํ–ฅ์œผ๋กœ ํ™•๋Œ€๋œ๋‹ค.


32.2 ๋‚ด๋ถ€๊ฒฐํ•ฉ

INNER JOIN์€ ON ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, ON ์ ˆ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•
1. ์ฒซ๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
INNER JOIN ๋‘๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
ON ์กฐ๊ฑด

2. ์ฒซ๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
JOIN ๋‘๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
ON ์กฐ๊ฑด

ON ์ ˆ์—์„œ๋Š” WHERE ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ‘œ์ค€ SQL๊ณผ๋Š” ๋‹ฌ๋ฆฌ MySQL์—์„œ๋Š” JOIN, INNER JOIN, CROSS JOIN์ด ๋ชจ๋‘ ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ)

SELECT *
FROM Reservation
INNER JOIN Customer
ON Reservation.Name = Customer.Name;

SELECT *
FROM Reservation
JOIN Customer
ON Reservation.Name = Customer.Name;

INNER JOIN์˜ ๊ฒฝ์šฐ์—๋Š” ์•ž์„œ ์‚ดํŽด๋ณธ ํ‘œ์ค€ SQL ๋ฐฉ์‹๊ณผ๋Š” ๋ณ„๋„๋กœ MySQL์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์ด ๋”ฐ๋กœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์•ž์„œ ์‚ดํŽด๋ณธ INNER JOIN ์˜ˆ์ œ์™€ ๊ฐ™์€ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
๊ฒฐ๊ณผ) 

์œ„ ์˜ˆ์ œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ JOIN์˜ ๊ฒฐ๊ณผ๋Š” ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.
MySQL ์ˆ˜์—… ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Reservation ํ…Œ์ด๋ธ”๊ณผ Customer ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Reservation ํ…Œ์ด๋ธ”
Customer ํ…Œ์ด๋ธ”

INNER JOIN์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฒค ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 


์˜ˆ์ œ)

SELECT *
FROM Reservation, Customer
WHERE Reservation.Name = Customer.Name;

์œ„์˜ ์˜ˆ์ œ์ฒ˜๋Ÿผ ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์ด ๊ธธ๊ฑฐ๋‚˜ ๋ณต์žกํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ณ„์นญ(alias)์„ ์‚ฌ์šฉํ•˜์—ฌ SQL ๊ตฌ๋ฌธ์„ ๊ฐ„๋žตํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์•ž์˜ ์˜ˆ์ œ๋ฅผ ๋ณ„์นญ(alias)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋žตํ™”ํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
์˜ˆ์ œ)

SELECT *
FROM Reservation AS r, Customer AS c
WHERE r.Name = c.Name;

์ฐธ๊ณ : http://www.tcpschool.com/mysql/mysql_multipleTable_join


์˜ˆ์ œ 7-6) ์ƒํ’ˆ ํ…Œ์ด๋ธ” ์ž‘์„ฑํ•˜๊ธฐ

CREATE TABLE ์ƒํ’ˆ(
    ์ƒํ’ˆ์ฝ”๋“œ CHAR(4) NOT NULL,
    ์ƒํ’ˆ๋ช… VARCHAR(30),
    ๋ฉ”์ด์ปค๋ช… VARCHAR(30),
    ๊ฐ€๊ฒฉ INTEGER,
    ์ƒํ’ˆ๋ถ„๋ฅ˜ VARCHAR(30),
    PRIMARY KEY (์ƒํ’ˆ์ฝ”๋“œ)
 );


์˜ˆ์ œ 7-7) ์žฌ๊ณ ์ˆ˜ ํ…Œ์ด๋ธ” ์ž‘์„ฑํ•˜๊ธฐ 

CREATE TABLE ์žฌ๊ณ ์ˆ˜(
    ์ƒํ’ˆ์ฝ”๋“œ CHAR(4),
    ์ž…๊ณ ๋‚ ์งœ DATE,
    ์žฌ๊ณ ์ˆ˜ INTERGER
);


์˜ˆ์ œ 7-8) ์ƒํ’ˆ ํ…Œ์ด๋ธ”๊ณผ ์žฌ๊ณ ์ˆ˜ ํ…Œ์ด๋ธ”์„ ๊ต์ฐจ๊ฒฐํ•ฉํ•˜๊ธฐ

SELECT * FROM ์ƒํ’ˆ, ์žฌ๊ณ ์ˆ˜;

์ƒํ’ˆ ํ…Œ์ด๋ธ”, ์žฌ๊ณ ์ˆ˜ ํ…Œ์ด๋ธ”


์˜ˆ์ œ 7-9) ์ƒํ’ˆ์ฝ”๋“œ๊ฐ€ ๊ฐ™์€ ํ–‰์„ ๊ฒ€์ƒ‰ํ•˜๊ธฐ

SELECT * FROM ์ƒํ’ˆ, ์žฌ๊ณ ์ˆ˜
	WHERE ์ƒํ’ˆ.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ;


์˜ˆ์ œ 7-10) ๊ฒ€์ƒ‰ํ•  ํ–‰๊ณผ ๋ฐ˜ํ™˜ํ•  ์—ด ์ œํ•œํ•˜๊ธฐ

SELECT ์ƒํ’ˆ.์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜.์žฌ๊ณ ์ˆ˜ FROM ์ƒํ’ˆ, ์žฌ๊ณ ์ˆ˜
  WHERE ์ƒํ’ˆ.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ
    AND ์ƒํ’ˆ.์ƒํ’ˆ๋ถ„๋ฅ˜ = '์‹๋ฃŒํ’ˆ';


32.3 INNER JOIN์œผ๋กœ ๋‚ด๋ถ€ ๊ฒฐํ•ฉํ•˜๊ธฐ p. 294

SELECT ์ƒํ’ˆ.์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜.์žฌ๊ณ ์ˆ˜
  FROM ์ƒํ’ˆ(ํ…Œ์ด๋ธ”๋ช…1) INNER JOIN ์žฌ๊ณ ์ˆ˜(ํ…Œ์ด๋ธ”๋ช…2)
    ON ์ƒํ’ˆ.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ (๊ฒฐํ•ฉ์กฐ๊ฑด)
  WHERE ์ƒํ’ˆ.์ƒํ’ˆ๋ถ„๋ฅ˜ = '์‹๋ฃŒํ’ˆ';

๋‚ด๋ถ€๊ฒฐํ•ฉ

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…1 INNER JOIN ํ…Œ์ด๋ธ”๋ช…2 ON ๊ฒฐํ•ฉ์กฐ๊ฑด

Point: INNER JOIN์œผ๋กœ ๋‘ ๊ฐœ ํ…Œ์ด๋ธ”์„ ๊ฐ€๋กœ๋กœ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋‘ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.

๋‘ ํ…Œ์ด๋ธ”์˜ ์ƒํ’ˆ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์€ ์กฐ๊ฑด์ด๋‹ค. 


32.4 ๋‚ด๋ถ€๊ฒฐํ•ฉ์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ p. 295

์˜ˆ์ œ 7-11) ๋ฉ”์ด์ปค ํ…Œ์ด๋ธ” ์ž‘์„ฑํ•˜๊ธฐ

CREATE TABLE ๋ฉ”์ด์ปค(
    ๋ฉ”์ด์ปค์ฝ”๋“œ CHAR(4) NOT NULL,
    ๋ฉ”์ด์ปค๋ช… VARCHAR(30),
    PRIMARY KEY (๋ฉ”์ด์ปค์ฝ”๋“œ)
);

๋ฉ”์ด์ปค ํ…Œ์ด๋ธ”


์˜ˆ์ œ 7-12) ์ƒํ’ˆ ํ…Œ์ด๋ธ”๊ณผ ๋ฉ”์ด์ปค ํ…Œ์ด๋ธ”์„ ๋‚ด๋ถ€๊ฒฐํ•ฉํ•˜๊ธฐ 

์ƒํ’ˆ2 ํ…Œ์ด๋ธ”

SELECT S.์ƒํ’ˆ๋ช…, M.๋ฉ”์ด์ปค๋ช…
    FROM ์ƒํ’ˆ2 S INNER JOIN ๋ฉ”์ด์ปค M
    	ON S.๋ฉ”์ด์ปค์ฝ”๋“œ = M.๋ฉ”์ด์ปค์ฝ”๋“œ;

๋ฉ”์ด์ปค ํ…Œ์ด๋ธ”์—๋Š” 'M', ์ƒํ’ˆ ํ…Œ์ด๋ธ”์—๋Š” 'S'๋ผ๋Š” ์งง์€ ๋ณ„๋ช…์„ ๋ถ™์˜€๋‹ค.

๋ณ„๋ช….๋ณ€์ˆ˜๋ช…
S.์ƒํ’ˆ๋ช… (from Table ์ƒํ’ˆ2)
M.๋ฉ”์ด์ปค๋ช… (from Table ๋ฉ”์ด์ปค)


1) ์™ธ๋ถ€ํ‚ค
๋ฉ”์ด์ปค ํ…Œ์ด๋ธ”์˜ ๋ฉ”์ด์ปค์ฝ”๋“œ๋Š” ๊ธฐ๋ณธํ‚ค์ด๋‹ค. 
๊ทธ์— ๋น„ํ•ด ์ƒํ’ˆ ํ…Œ์ด๋ธ”์˜ ๋ฉ”์ด์ปค์ฝ”๋“œ๋Š” '์™ธ๋ถ€ํ‚ค'๋ผ ๋ถˆ๋ฆฐ๋‹ค. ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์—ด์ด ์™ธ๋ถ€ํ‚ค๊ฐ€ ๋œ๋‹ค. 

2) ์ž๊ธฐ ๊ฒฐํ•ฉ(Self Join) 

ํ…Œ์ด๋ธ”์— ๋ณ„๋ช…์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ๊ฐ™์€ ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


์˜ˆ์ œ 7-13) ์ƒํ’ˆ ํ…Œ์ด๋ธ”์„ ์ž๊ธฐ๊ฒฐํ•ฉํ•˜๊ธฐ

SELECT S1.์ƒํ’ˆ๋ช…, S2.์ƒํ’ˆ๋ช…
    FROM ์ƒํ’ˆ S1 INNER JOIN ์ƒํ’ˆ S2
	ON S1.์ƒํ’ˆ์ฝ”๋“œ = S2.์ƒํ’ˆ์ฝ”๋“œ;

์ž๊ธฐ ๊ฒฐํ•ฉ์—์„œ๋Š” ๊ฒฐํ•ฉ์˜ ์ขŒ์šฐ๊ฐ€ ๊ฐ™์€ ํ…Œ์ด๋ธ”์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ๋ณ„๋ช…์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
์ž๊ธฐ ๊ฒฐํ•ฉ์€ ์ž๊ธฐ ์ž์‹ ์˜ ๊ธฐ๋ณธํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์—ด์„ ์ž๊ธฐ ์ž์‹ ์ด ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ์— ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

select ์ƒํ’ˆ๋ช…, ๋ฉ”์ด์ปค๋ช…, S.๋ฉ”์ด์ปค์ฝ”๋“œ
from ์ƒํ’ˆ2 S, ๋ฉ”์ด์ปค M
where S.๋ฉ”์ด์ปค์ฝ”๋“œ = M.๋ฉ”์ด์ปค์ฝ”๋“œ
and S.๋ฉ”์ด์ปค์ฝ”๋“œ = 'M001';

select ์ƒํ’ˆ๋ช…, ๋ฉ”์ด์ปค๋ช…, S.๋ฉ”์ด์ปค์ฝ”๋“œ
from ์ƒํ’ˆ2 S inner join ๋ฉ”์ด์ปค M
on S.๋ฉ”์ด์ปค์ฝ”๋“œ = M.๋ฉ”์ด์ปค์ฝ”๋“œ
where S.๋ฉ”์ด์ปค์ฝ”๋“œ = 'M001';

๋‘ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.


32.5 ์™ธ๋ถ€๊ฒฐํ•ฉ

LEFT JOIN
LEFT JOIN์€ ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ, ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์„ ์กฐํ•ฉํ•˜๋Š” JOIN์ž…๋‹ˆ๋‹ค. 

์ด๋•Œ ON ์ ˆ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ ˆ์ฝ”๋“œ์˜ ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ ๊ฐ’์€ ๋ชจ๋‘ NULL๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•
์ฒซ๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
LEFT JOIN ๋‘๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
ON ์กฐ๊ฑด

ON ์ ˆ์—์„œ๋Š” WHERE ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ๋Š” Reservation ํ…Œ์ด๋ธ”์˜ Name ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ Customer ํ…Œ์ด๋ธ”์˜ Name ํ•„๋“œ์™€ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋งŒ์„ LEFT JOIN์œผ๋กœ ๊ฐ€์ ธ์˜จ ํ›„, ๊ทธ ์ค‘์—์„œ ReserveDate ํ•„๋“œ์˜ ๊ฐ’์ด 2016๋…„ 02์›” 01์ผ ์ดํ›„์ธ ๋ ˆ์ฝ”๋“œ๋งŒ์„ ์„ ํƒํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
์˜ˆ์ œ) 

SELECT *
FROM Reservation
LEFT JOIN Customer
ON Reservation.Name = Customer.Name
WHERE ReserveDate > '2016-02-01';

๊ฒฐ๊ณผ) 

์œ„์˜ ์˜ˆ์ œ์—์„œ ๋‘ ๊ฐœ์˜ Name ๊ฐ’์ด ์ผ์น˜ํ•˜๋ฉด, INNER JOIN๊ณผ ๊ฐ™์ด ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‘ ๊ฐœ์˜ Name ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” Customer ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ NULL๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

LEFT JOIN์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฒค ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


RIGHT JOIN
RIGHT JOIN์€ LEFT ์กฐ์ธ๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ, ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์„ ์กฐํ•ฉํ•˜๋Š” JOIN์ž…๋‹ˆ๋‹ค. 
 
์ด๋•Œ ON ์ ˆ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ ˆ์ฝ”๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ ๊ฐ’์€ ๋ชจ๋‘ NULL๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•
์ฒซ๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
RIGHT JOIN ๋‘๋ฒˆ์งธํ…Œ์ด๋ธ”์ด๋ฆ„
ON ์กฐ๊ฑด

ON ์ ˆ์—์„œ๋Š” WHERE ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ๋Š” Customer ํ…Œ์ด๋ธ”์˜ Name ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ Reservation ํ…Œ์ด๋ธ”์˜ Name ํ•„๋“œ์™€ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋งŒ์„ RIGHT JOIN์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ)

SELECT *
FROM Reservation
RIGHT JOIN Customer
ON Reservation.Name = Customer.Name;

๊ฒฐ๊ณผ) 

์œ„์˜ ์˜ˆ์ œ์—์„œ ๋‘ ๊ฐœ์˜ Name ๊ฐ’์ด ์ผ์น˜ํ•˜๋ฉด, INNER JOIN๊ณผ ๊ฐ™์ด ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‘ ๊ฐœ์˜ Name ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” Reservation ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ NULL๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

RIGHT JOIN์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฒค ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ : http://www.tcpschool.com/mysql/mysql_multipleTable_join


์™ธ๋ถ€๊ฒฐํ•ฉ์€ '์–ด๋Š ํ•œ ์ชฝ์—๋งŒ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐํ–‰์„ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃฐ์ง€'๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฐํ•ฉ ๋ฐฉ๋ฒ•์ด๋‹ค.

์˜ˆ์ œ 7-14) ๋‚ด๋ถ€ ๊ฒฐํ•ฉ์—์„œ๋Š” ์ƒํ’ˆ์ฝ”๋“œ๊ฐ€ 0009์ธ ์ƒํ’ˆ์ด ์ œ์™ธ๋œ๋‹ค.

SELECT ์ƒํ’ˆ3.์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜.์žฌ๊ณ ์ˆ˜
FROM ์ƒํ’ˆ3 INNER JOIN ์žฌ๊ณ ์ˆ˜
ON ์ƒํ’ˆ3.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ
WHERE ์ƒํ’ˆ3.์ƒํ’ˆ๋ถ„๋ฅ˜ = '์‹๋ฃŒํ’ˆ';

์˜ˆ์ œ 7-15) ์™ธ๋ถ€๊ฒฐํ•ฉ์œผ๋กœ ์ƒํ’ˆ์ฝ”๋“œ 0009์ธ ์ƒํ’ˆ๋„ ๊ฒฐ๊ณผ์— ํฌํ•จํ•˜๊ธฐ

SELECT ์ƒํ’ˆ3.์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜.์žฌ๊ณ ์ˆ˜
  FROM ์ƒํ’ˆ3 LEFT JOIN ์žฌ๊ณ ์ˆ˜
    ON ์ƒํ’ˆ3.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ
  WHERE ์ƒํ’ˆ3.์ƒํ’ˆ๋ถ„๋ฅ˜ = '์‹๋ฃŒํ’ˆ';

Point: LEFT JOIN, RIGHT JOIN์œผ๋กœ ์™ธ๋ถ€๊ฒฐํ•ฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 


- ๊ตฌ์‹๋ฐฉ๋ฒ•์—์„œ์˜ ์™ธ๋ถ€๊ฒฐํ•ฉ๊ณผ ํ‘œ์ค€ SQL 
์˜ˆ์ œ 7-16) Oracle์—์„œ ๊ตฌ์‹ ์™ธ๋ถ€ ๊ฒฐํ•ฉ์œผ๋กœ 0009์˜ ์ƒํ’ˆ์„ ๊ฒฐ๊ณผ์— ํฌํ•จํ•˜๊ธฐ

๊ตฌ์‹ ๊ฒฐํ•ฉ๋ฐฉ๋ฒ•์—๋Š” WHERE๊ตฌ๋กœ ๊ฒฐํ•ฉ ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๊ณ  ์™ธ๋ถ€๊ฒฐํ•ฉ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ (+) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (Oracle)

SELECT ์ƒํ’ˆ3.์ƒํ’ˆ๋ช…, ์žฌ๊ณ ์ˆ˜.์žฌ๊ณ ์ˆ˜
  FROM ์ƒํ’ˆ3, ์žฌ๊ณ ์ˆ˜
  WHERE ์ƒํ’ˆ3.์ƒํ’ˆ์ฝ”๋“œ = ์žฌ๊ณ ์ˆ˜.์ƒํ’ˆ์ฝ”๋“œ (+) -- left join
    AND ์ƒํ’ˆ3.์ƒํ’ˆ๋ถ„๋ฅ˜ = '์‹๋ฃŒํ’ˆ';

 

ํ˜„์žฌ๋Š” ํ‘œ์ค€ํ™”๋กœ ์ธํ•ด ๋‚ด๋ถ€๊ฒฐํ•ฉ์€ INNER JOIN, ์™ธ๋ถ€๊ฒฐํ•ฉ์€ LEFT JOIN์ด๋‚˜ RIGHT JOIN์„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒ์žฅํ•œ๋‹ค.

Point: ๊ตฌ์‹ ๊ฒฐํ•ฉ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.


33๊ฐ• ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ


33.1 ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ

Point: ๋ฆด๋ ˆ์ด์…˜์€ ํ…Œ์ด๋ธ”์„ ๋งํ•œ๋‹ค. 


33.2 ๊ด€๊ณ„ํ˜• ๋ชจ๋ธ๊ณผ SQL

1. ํ•ฉ์ง‘ํ•ฉ (Union)
2. ์ฐจ์ง‘ํ•ฉ (Difference)
3. ๊ต์ง‘ํ•ฉ (Intersection)
4. ๊ณฑ์ง‘ํ•ฉ (Cartesian product)
5. ์„ ํƒ (Selection)
6. ํˆฌ์˜ (Projection)
7. ๊ฒฐํ•ฉ (Join)


 

728x90
๋ฐ˜์‘ํ˜•
Comments