[MySQL] 5. SQL - JOIN

sorzzzzyยท2021๋…„ 8์›” 13์ผ
0

MySQL

๋ชฉ๋ก ๋ณด๊ธฐ
5/8
post-thumbnail
post-custom-banner

๐Ÿท JOIN

JOIN์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•ด ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ  ์‹ถ์„ ๋–„ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ๋ฌธ!
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ฒฐํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค.
์กฐ์ธ์„ ํ• ๋•Œ์—๋Š” ๋‘๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์ด ํ•„์š”ํ•˜๋ฉฐ, ์ปฌ๋Ÿผ ๊ฐ’์ด ๊ฐ™์€ ๊ธฐ๋ณธํ‚ค์™€ ์™ธ๋ž˜ํ‚ค๊ฐ€ ์ผ์น˜ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด์–ด์•ผ ์กฐ์ธ์ด ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์ถœ์ œ๋˜๋Š” ๋ถ€๋ถ„์ด๊ธฐ๋„ ํ•จ !

1) LEFT JOIN

LEFT JOIN์€ ํ…Œ์ด๋ธ” A๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์กฐ์ธ

SELECT A.์ปฌ๋Ÿผ 1
	, ...
    	, B.์ปฌ๋Ÿผ 1
    	, ...
FROM ํ…Œ์ด๋ธ”1 A
LEFT JOIN ํ…Œ์ด๋ธ”2 B
ON A.์ปฌ๋Ÿผ1 = B.์ปฌ๋Ÿผ1;

FROM ์ ˆ์—์„œ ๊ธฐ์ค€์ด ๋˜๋Š” ํ…Œ์ด๋ธ”์„ ์ž…๋ ฅํ•˜๊ณ  LEFT JOIN ์ ˆ์—์„œ ์กฐ์ธ๋˜๋Š” ํ…Œ์ด๋ธ”์„ ์ž…๋ ฅํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ON ์ ˆ์—๋Š” ์ผ์น˜ ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜์—ฌ ํ…Œ์ด๋ธ”A๊ณผ ํ…Œ์ด๋ธ”B ์นผ๋Ÿผ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š” ํ–‰๋งŒ์„ ์กฐํšŒํ•˜๋„๋ก ํ•œ๋‹ค.

์กฐํšŒ ๊ฒฐ๊ณผ๋กœ USER_ID, QUEST_TITLE ์นผ๋Ÿผ ์ค‘์—์„œ T_USER์˜ USER_ID๊ฐ’์ด T_QUEST์˜ USER_ID ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ํ–‰์ด ์กฐํšŒ๋œ๋‹ค.


2) RIGHT JOIN

RIGHT JOIN์€ ํ…Œ์ด๋ธ” B๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์กฐ์ธ

SELECT A.์ปฌ๋Ÿผ 1
	, ...
    	, B.์ปฌ๋Ÿผ 1
    	, ...
FROM ํ…Œ์ด๋ธ”1 A
RIGHT JOIN ํ…Œ์ด๋ธ”2 B
ON A.์ปฌ๋Ÿผ1 = B.์ปฌ๋Ÿผ1;

RIGHT JOIN์—์„œ๋Š” T_USER์™€ T_QUEST์˜ ์ž…๋ ฅ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋ฉด LEFT JOIN๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์กฐํšŒ๋œ๋‹ค


3) INNER JOIN

INNER JOIN์€ ๋‘ ํ…Œ์ด๋ธ”์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰๋งŒ ์กฐํšŒํ•œ๋‹ค.

SELECT A.์ปฌ๋Ÿผ 1
	, ...
    	, B.์ปฌ๋Ÿผ 1
    	, ...
FROM ํ…Œ์ด๋ธ”1 A
INNER JOIN ํ…Œ์ด๋ธ”2 B
ON A.์ปฌ๋Ÿผ1 = B.์ปฌ๋Ÿผ1;
[WHERE ์กฐ๊ฑด]

์ฐธ๊ณ  ์ž๋ฃŒ 1



๐Ÿท ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(SQL-Kit JOIN)๋ฌธ์ œ ํ’€์ด


1. ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ

โœ”๏ธ ๋ฌธ์ œ : ์ž…์–‘์„ ๊ฐ„ ๊ธฐ๋ก์€ ์žˆ๋Š”๋ฐ(ANIMAL_OUTS), ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๊ธฐ๋ก(ANIMAL_INS)์ด ์—†๋Š” ๋™๋ฌผ์˜ ID์™€ ์ด๋ฆ„์„ ID ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

  • ๊ฐ„ ๊ธฐ๋ก(outs)๋ฅผ ๊ธฐ์ค€์œผ๋กœ left join ํ•˜๊ธฐ
  • on ํ‚ค์›Œ๋“œ ๋’ค์— ์ผ์น˜ ์กฐ๊ฑด ๋ช…์‹œ
  • where ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐ๊ฑด ๋ช…์‹œ (๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๊ธฐ๋ก์ด ์—†๋Š”, ASC๋Š” ์ƒ๋žต๊ฐ€๋Šฅ)
SELECT 
    ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME
FROM 
    ANIMAL_OUTS LEFT JOIN ANIMAL_INS
ON
    ANIMAL_OUTS.ANIMAL_ID = ANIMAL_INS.ANIMAL_ID
WHERE
    ANIMAL_INS.ANIMAL_ID IS NULL

2. ์žˆ์—ˆ๋Š”๋ฐ์š” ์—†์—ˆ์Šต๋‹ˆ๋‹ค

โœ”๏ธ ๋ฌธ์ œ : ๊ด€๋ฆฌ์ž์˜ ์‹ค์ˆ˜๋กœ ์ผ๋ถ€ ๋™๋ฌผ์˜ ์ž…์–‘์ผ์ด ์ž˜๋ชป ์ž…๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ˜ธ ์‹œ์ž‘์ผ๋ณด๋‹ค ์ž…์–‘์ผ์ด ๋” ๋น ๋ฅธ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ๋ณดํ˜ธ ์‹œ์ž‘์ผ์ด ๋น ๋ฅธ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

  • inner join ์‚ฌ์šฉํ•˜๊ธฐ
  • on ํ‚ค์›Œ๋“œ ๋’ค์— ์ผ์น˜ ์กฐ๊ฑด ๋ช…์‹œ
  • where ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐ๊ฑด ๋ช…์‹œ (๋ณดํ˜ธ ์‹œ์ž‘์ผ๋ณด๋‹ค ์ž…์–‘์ผ์ด ๋” ๋น ๋ฅธ)
  • order by ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐํšŒ ์กฐ๊ฑด ๋ช…์‹œ (๋ณดํ˜ธ ์‹œ์ž‘์ผ์ด ๋น ๋ฅธ ์ˆœ)
SELECT 
    ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME
FROM 
    ANIMAL_OUTS INNER JOIN ANIMAL_INS
ON
    ANIMAL_OUTS.ANIMAL_ID = ANIMAL_INS.ANIMAL_ID
WHERE
    ANIMAL_OUTS.DATETIME < ANIMAL_INS.DATETIME
ORDER BY
    ANIMAL_INS.DATETIME ASC

3. ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ

โœ”๏ธ ๋ฌธ์ œ : ์•„์ง ์ž…์–‘์„ ๋ชป ๊ฐ„ ๋™๋ฌผ ์ค‘, ๊ฐ€์žฅ ์˜ค๋ž˜ ๋ณดํ˜ธ์†Œ์— ์žˆ์—ˆ๋˜ ๋™๋ฌผ 3๋งˆ๋ฆฌ์˜ ์ด๋ฆ„๊ณผ ๋ณดํ˜ธ ์‹œ์ž‘์ผ์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ๋ณดํ˜ธ ์‹œ์ž‘์ผ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • left join ์‚ฌ์šฉํ•˜๊ธฐ (INS๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ๋“ค์–ด์˜ค๊ธด ํ–ˆ๋Š”๋ฐ ๋‚˜๊ฐ€์ง€ ๋ชปํ•œ ๊ฒƒ์ด๋ฏ€๋กœ)
  • on ํ‚ค์›Œ๋“œ ๋’ค์— ์ผ์น˜ ์กฐ๊ฑด ๋ช…์‹œ
  • where ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐ๊ฑด ๋ช…์‹œ (์•„์ง ์ž…์–‘์„ ๊ฐ€์ง€ ๋ชปํ•œ)
  • order by ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐํšŒ ์กฐ๊ฑด ๋ช…์‹œ (๋ณดํ˜ธ ์‹œ์ž‘์ผ์ด ๋น ๋ฅธ ์ˆœ์œผ๋กœ 3๋งˆ๋ฆฌ)
SELECT 
    ANIMAL_INS.NAME, ANIMAL_INS.DATETIME
FROM 
    ANIMAL_INS LEFT JOIN ANIMAL_OUTS 
ON
    ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
WHERE
    ANIMAL_OUTS.ANIMAL_ID IS NULL
ORDER BY
    ANIMAL_INS.DATETIME ASC LIMIT 3

4. ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผ

โœ”๏ธ ๋ฌธ์ œ : ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™” ์ˆ˜์ˆ ์„ ๊ฑฐ์นœ ๋™๋ฌผ ์ •๋ณด๋ฅผ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋˜์ง€ ์•Š์•˜์ง€๋งŒ, ๋ณดํ˜ธ์†Œ๋ฅผ ๋‚˜๊ฐˆ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋œ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ƒ๋ฌผ ์ข…, ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
(๐Ÿ“Œ ํŒ : ์ค‘์„ฑํ™”๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์€ ๋™๋ฌผ์€ ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€์— Intact, ์ค‘์„ฑํ™”๋ฅผ ๊ฑฐ์นœ ๋™๋ฌผ์€ Spayed ๋˜๋Š” Neutered๋ผ๊ณ  ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.)

  • left join ์‚ฌ์šฉํ•˜๊ธฐ (OUTS๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ๋“ค์–ด์˜ฌ ๋‹น์‹œ์—๋Š” ์•„๋‹ˆ์—ˆ์ง€๋งŒ ๋‚˜๊ฐˆ ๋•Œ๋Š” ์ค‘์„ฑํ™” ๋์œผ๋ฏ€๋กœ)
  • on ํ‚ค์›Œ๋“œ ๋’ค์— ์ผ์น˜ ์กฐ๊ฑด ๋ช…์‹œ
  • where ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐ๊ฑด ๋ช…์‹œ (๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋˜์ง€ ์•Š์•˜์ง€๋งŒ, ๋ณดํ˜ธ์†Œ๋ฅผ ๋‚˜๊ฐˆ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋œ)
  • order by ํ‚ค์›Œ๋“œ ๋’ค์— ์กฐํšŒ ์กฐ๊ฑด ๋ช…์‹œ (์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒ)
SELECT  ANIMAL_OUTS.ANIMAL_ID, 
        ANIMAL_OUTS.ANIMAL_TYPE, 
        ANIMAL_OUTS.NAME 
FROM 
    ANIMAL_OUTS LEFT JOIN ANIMAL_INS 
ON 
    ANIMAL_OUTS.ANIMAL_ID=ANIMAL_INS.ANIMAL_ID 
WHERE ANIMAL_INS.SEX_UPON_INTAKE LIKE 'Intact%' AND 
        (ANIMAL_OUTS.SEX_UPON_OUTCOME LIKE 'Spayed%' OR 
         ANIMAL_OUTS.SEX_UPON_OUTCOME LIKE 'Neutered%')
ORDER BY ANIMAL_OUTS.ANIMAL_ID

๐Ÿ’ก LIKE ํ‚ค์›Œ๋“œ๋Š” ๋ถ€๋ถ„์ ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ์นผ๋Ÿผ์„ ์ฐพ๊ณ ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค

  • --A๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE 'A%'
  • --A๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '%A'
  • --A๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '%A%'
  • --A๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‘๊ธ€์ž ๋ฌธ์ž ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE 'A_'
  • --์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'A''๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž์—ด ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE'[^A]'
  • --์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'A'๋˜๋Š”'B'๋˜๋Š”'C'์ธ ๋ฌธ์ž์—ด ์ฐพ๊ธฐ--
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '[ABC]'
    SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '[A-C]'
  • --'๊น€'์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ์› ์กฐํšŒ
    SELECT * FROM My_Talbe WHERE Nm_Kor LIKE '๊น€%'
  • --๊น€์ด ๋“ค์–ด๊ฐ€๋Š” ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ์› ์กฐํšŒ
    SELECT * FROM My_Talbe WHERE Nm_Kor LIKE '%๊น€%'
  • --๊น€์œผ๋กœ ๋๋‚˜๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ ์กฐํšŒ
    SELECT No_Emp FROM My_Talbe WHERE Nm_Kor LIKE '%๊น€'

์ฐธ๊ณ  ์ž๋ฃŒ 2

profile
Backend Developer
post-custom-banner

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