๐Ÿ’พ JOIN์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

suRanยท2022๋…„ 7์›” 15์ผ
0

๐Ÿ’พ MySQL

๋ชฉ๋ก ๋ณด๊ธฐ
4/9
post-thumbnail

๋ณธ ํฌ์ŠคํŒ…์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฏธ๋‹ˆ ๋ฐ๋ธŒ ์ฝ”์Šค๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ
ํ•™์Šต์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค.

1. JOIN์€?

JOIN์ด๋ž€ ์Šคํƒ€ ์Šคํ‚ค๋งˆ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ RDBMS์—์„œ
๊ฐ ํ…Œ์ด๋ธ”์„ ๋ณ‘ํ•ฉํ•ด์„œ ์™„์ „ํ•œ ์ •๋ณด๋ฅผ ์–ป๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ด๋‹ค.

์Šคํƒ€ ์Šคํ‚ค๋งˆ ๋ชจ๋ธ์€ ์ €์žฅ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๊ฐ€ ๋œํ•˜๊ณ 
ํŠน์ • ์—”ํ‹ฐํ‹ฐ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์˜์กด์„ฑ์—†์ด ์‰ฝ๊ฒŒ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜์กด์„ฑ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?
๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)์—์„œ ์˜์กด์„ฑ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ํด๋ž˜์Šค ๊ฐ„ ์˜์กด ๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
์ฆ‰, ํ•œ ํด๋ž˜์Šค๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋‹ค๋ฅธ ํด๋ž˜์Šค๋„ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค.

์Šคํƒ€ ์Šคํ‚ค๋งˆ ๋ชจ๋ธ์—์„œ๋Š” ๊ทธ๋Ÿฐ ๊ฐ์ฒด(ํ…Œ์ด๋ธ”)๊ฐ„ ์˜ํ–ฅ ์—†์ด ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์Šคํƒ€ ์Šคํ‚ค๋งˆ๋ฅผ ์ด์šฉํ•œ DB์ผ ๊ฒฝ์šฐ
์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— ์ •๋ณด๊ฐ€ ๋ถ„์‚ฐ๋˜์–ด ์žˆ์„ ํ™•๋ฅ ์ด ๋†’์œผ๋ฏ€๋กœ
์™„์ „ํ•œ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” JOIN ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋‹ค.


JOIN์ด๋ž€?
JOIN์€ ์–‘์ชฝ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ SELECT ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

JOIN์˜ ๋ฐฉ์‹์— ๋”ฐ๋ผ

  • ์–ด๋–ค ๋ ˆ์ฝ”๋“œ๊ฐ€ ์„ ํƒ๋˜๋Š”๊ฐ€?

  • ์–ด๋–ค ํ•„๋“œ๊ฐ€ ์ฑ„์›Œ์ง€๋Š”๊ฐ€?

๋‘ ๊ฐ€์ง€๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.





2. JOIN ์‹œ ์ฃผ์˜ํ•  ์ 

  • ์ค‘๋ณต ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†์–ด์•ผ ํ•œ๋‹ค.

  • JOIN ๋Œ€์ƒ ํ…Œ์ด๋ธ”์— Primary Key๊ฐ€ ์ œ๋Œ€๋กœ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • ์–ด๋Š ํ…Œ์ด๋ธ”์„ FROM์ ˆ์— ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค.

  • JOIN ํ…Œ์ด๋ธ” ๊ฐ„ ๊ด€๊ณ„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

    • One to one *
    • One to many (ex: ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”๊ณผ ์ฃผ๋ฌธ ๋ฌผํ’ˆ ํ…Œ์ด๋ธ”๋“ค )
    • Many to one
    • Many to many




3. JOIN ๋ฌธ๋ฒ•

JOIN ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

SELECT A.*, B.*
FROM table1 A
[INNER | LEFT | RIGHT | CROSS ] JOIN table2 B ON A.number = B.id_number
WHERE table1์˜ ์กฐ๊ฑด;
  • JOIN ์•ž์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋””ํดํŠธ๋กœ INNER JOIN ์„ ์‚ฌ์šฉํ•œ๋‹ค.

  • JOIN์—์„œ WHERE ์กฐ๊ฑด๋ฌธ์€ ๋ณดํ†ต FROM์ ˆ ํ…Œ์ด๋ธ”์˜ ์„ ํƒ์กฐ๊ฑด์ด๋‹ค.


SELECT A.*, B.*
FROM table1 A
[INNER | LEFT | RIGHT | CROSS ] JOIN table2 B ON A.number = B.id_number
WHERE A.timestamp >= '2019-01-01';
  • ์œ„ ์ฝ”๋“œ์—์„œ๋Š” table1 ์˜ timestamp๊ฐ€ 2019-01-01 ์ด์ƒ์ธ ๋ ˆ์ฝ”๋“œ๋งŒ JOIN๋Œ€์ƒ์ด ๋œ๋‹ค.


๋‹ค์Œ ์‚ฌ์ง„์„ ์ฐธ๊ณ ํ•˜๋ฉฐ JOIN์˜ ์ข…๋ฅ˜๋ฅผ ํ•™์Šตํ•ด๋ณด์ž.

[์ด๋ฏธ์ง€ ์ถœ์ฒ˜] https://theartofpostgresql.com/blog/2019-09-sql-joins/



๐Ÿ’ฟ ์‹ค์Šต์— ์‚ฌ์šฉํ•  ํ…Œ์ด๋ธ”

vital ํ…Œ์ด๋ธ”

  • PK๋Š” vitalID

alert ํ…Œ์ด๋ธ”

  • PK๋Š” alertID





๐Ÿ’ฟ INNER JOIN *

์–‘์ชฝ์—์„œ ๋งค์นญ๋˜๋Š” ๋ ˆ์ฝ”๋“œ๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค.
(๋งค์นญ๋œ๋‹ค -> ON ๋’ค์— ์ œ์‹œ๋œ ๋ณ‘ํ•ฉ์กฐ๊ฑด์ด ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.)

๋งค์นญ์ด ์•ˆ ๋˜๋Š” ๋ ˆ์ฝ”๋“œ๋Š” JOIN ๋Œ€์ƒ์ด ์•„๋‹ˆ๊ธฐ์— ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ INNER JOIN์€ ํ•„๋“œ๊ฐ€ ๋ชจ๋‘ ์ฑ„์›Œ์ง„ ์ƒํƒœ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

(๋Œ€๋ถ€๋ถ„์˜ JOIN์€ INNER JOIN์ด๋‹ค.)


SELECT * FROM vital v
JOIN alert a ON v.vitalID = a.vitalID;



๐Ÿ’ฟ LEFT JOIN *

์™ผ์ชฝ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ์ „๋ถ€ ๋ฆฌํ„ดํ•œ๋‹ค.
์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ๋„˜์–ด์˜ค๋Š” ๋ ˆ์ฝ”๋“œ๋Š” ๋งค์นญ๋˜๋Š” ๊ฒƒ๋งŒ ํ•„๋“œ๊ฐ€ ์ฑ„์›Œ์ ธ์„œ ๋ฆฌํ„ดํ•œ๋‹ค.
๋งค์นญ์ด ์•ˆ ๋˜๋Š” ํ•„๋“œ๋Š” NULL๋กœ ๋‚˜์˜จ๋‹ค.

FROM์ ˆ์ด LEFT JOIN์—์„œ์˜ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์ด๋‹ค.


SELECT * FROM vital v
LEFT JOIN alert a ON v.vitalID = a.vitalID;




๐Ÿ’ฟ RIGHT JOIN

์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ์ „๋ถ€ ๋ฆฌํ„ดํ•œ๋‹ค.
์™ผ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ๋„˜์–ด์˜ค๋Š” ๋ ˆ์ฝ”๋“œ๋Š” ๋งค์นญ๋˜๋Š” ๊ฒƒ๋งŒ ํ•„๋“œ๊ฐ€ ์ฑ„์›Œ์ ธ์„œ ๋ฆฌํ„ดํ•œ๋‹ค.
๋งค์นญ์ด ์•ˆ ๋˜๋Š” ํ•„๋“œ๋Š” NULL๋กœ ๋‚˜์˜จ๋‹ค.

RIGHT JOIN์€ ์ˆœ์„œ๋งŒ ๋ฐ”๊พธ๋ฉด LEFT JOIN์œผ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


SELECT * FROM vital v
RIGHT JOIN alert a ON v.vitalID = a.vitalID;



(+) LEFT JOIN๊ณผ RIGHT JOIN

SELECT * FROM alert a
RIGHT JOIN vital v ON v.vitalID = a.vitalID;
  • ์œ„ ์ฝ”๋“œ์—์„œ FROM์ ˆ์— ์˜ค๋Š” ํ…Œ์ด๋ธ”์˜ ์ˆœ์„œ๋งŒ ๋ฐ”๊พธ๋ฉด LEFT JOIN์˜ ์˜ˆ์ œ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.



๐Ÿ’ฟ FULL JOIN (OUTER JOIN)

์–‘์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
์–‘์ชฝ์ด ๋ชจ๋‘ ๋งค์นญ๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋“  ํ•„๋“œ๋“ค์ด ์ฑ„์›Œ์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋ฆฌํ„ด๋˜๊ธฐ ๋•Œ๋ฌธ์—
๋งค์นญ์ด ์•ˆ ๋˜์–ด NULL๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์–‘์ชฝ ๋ชจ๋‘์—์„œ ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ‘‰ UNION์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

SELECT * FROM vital v
LEFT JOIN alert a ON v.vitalID = a.vitalID
UNION
SELECT * FROM vital v
RIGHT JOIN alert a ON v.vitalID = a.vitalID;
  • MySQL์—์„œ๋Š” FULL JOIN์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—
    LEFT JOIN๊ณผ RIGHT JOIN์„ UNIONํ•˜๋Š” ํ˜•ํƒœ๋กœ FULL JOIN์„ ํ‰๋‚ด๋‚ธ๋‹ค.

  • ๋‘ ๊ฐœ์˜ SELECT ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— LEFT JOIN๊ณผ RIGHT JOIN์—์„œ ๋ฐœ์ƒํ•œ ๋™์ผํ•œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.
    UNION ์—ฐ์‚ฐ์€ ๊ทธ๋ ‡๊ฒŒ ์–‘์ชฝ์—์„œ return๋˜๋Š” ์ค‘๋ณต ๋ ˆ์ฝ”๋“œ๋ฅผ ์—†์•ค ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.



๐Ÿ‘‰ UNION ALL์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

SELECT * FROM vital v
LEFT JOIN alert a ON v.vitalID = a.vitalID
UNION ALL
SELECT * FROM vital v
RIGHT JOIN alert a ON v.vitalID = a.vitalID;
  • UNION ALL์€ ์ค‘๋ณต ๋ ˆ์ฝ”๋“œ๋ฅผ ์—†์• ์ง€ ์•Š๋Š”๋‹ค.



๐Ÿ’ฟ CROSS JOIN (์นดํ‹ฐ์ „ ํ”„๋กœ๋•ํŠธ)

๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์— ๋Œ€ํ•ด์„œ ๋ณ‘ํ•ฉํ•œ๋‹ค.
๊ทธ๋ž˜์„œ JOIN ์กฐ๊ฑด์ด ์—†๋‹ค.

ํ…Œ์ด๋ธ” ๋‘ ๊ฐœ๊ฐ€ ๊ฐ๊ฐ A๊ฐœ, B๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ,
๋‘ ํ…Œ์ด๋ธ”์„ CROSS JOINํ•˜๋ฉด ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ์ˆ˜๋Š” A * B ๊ฐœ์ด๋‹ค.


SELECT * FROM vital v
CROSS JOIN alert a;  -- JOIN ๋ณ‘ํ•ฉ ์กฐ๊ฑด์ด ์—†๋‹ค.
  • ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์„ ๋ณด๋ฉด
    ์™ผ์ชฝ ๋ ˆ์ฝ”๋“œ ํ•˜๋‚˜(vitalID = 1)์— ์˜ค๋ฅธ์ชฝ ๋ ˆ์ฝ”๋“œ 3๊ฐœ๋ฅผ ์ „๋ถ€ ๋งค์นญํ•˜์—ฌ ๋ฆฌํ„ดํ–ˆ๋‹ค.
    ์™ผ์ชฝ ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ ํ•˜๋‚˜(vitalID = 2)์— ์˜ค๋ฅธ์ชฝ ๋ ˆ์ฝ”๋“œ 3๊ฐœ๋ฅผ ์ „๋ถ€ ๋งค์นญํ•˜์—ฌ ๋ฆฌํ„ดํ–ˆ๋‹ค.
    ๊ทธ๊ฒƒ์ด ์™ผ์ชฝ ๋ ˆ์ฝ”๋“œ์˜ ๋ ˆ์ฝ”๋“œ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋œ๋‹ค.
    ๊ทธ๋ž˜์„œ ์ถœ๋ ฅ๋˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ์ˆ˜๋Š” 3 * 4 ์ด๋‹ค.



๐Ÿ’ฟ SELF JOIN

๊ฐ™์€ ํ…Œ์ด๋ธ”๋ผ๋ฆฌ JOINํ•œ๋‹ค.
๋™์ผํ•œ ํ…Œ์ด๋ธ”์˜ alias (as)๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„œ ์ž๊ธฐ ์ž์‹ ๊ณผ JOINํ•œ๋‹ค.
JOIN ์กฐ๊ฑด์„ ์–ด๋–ป๊ฒŒ ์ฃผ๋Š”์ง€์— ๋”ฐ๋ผ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค.


SELECT * FROM vital v1
JOIN vital v2 ON v1.vitalID = v2.vitalID;



profile
๊ฐœ๋ฐœ ๊ณต๋ถ€๋ฅผ ํ•ด๋ผ

0๊ฐœ์˜ ๋Œ“๊ธ€