DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1) (MySQL) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1) (MySQL)

gimewn 2022. 10. 29. 00:22

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/59044

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ’ญ LEFT JOIN์„ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑด ์•Œ์•˜๋Š”๋ฐ, ์ž…์–‘ ๊ฐ€์ง€ ์•Š์€ ๋™๋ฌผ์„ ์–ด๋–ป๊ฒŒ ์ฒดํฌํ•ด์ค˜์•ผ ํ•˜๋Š”์ง€ ๊ณ ๋ฏผ์ด์—ˆ๋‹ค.

๐Ÿ’ญ ์•„๋ฌด๋ฆฌ ์ƒ๊ฐํ•ด๋„ ๊ฐ์ด ์•ˆ ์™€์„œ ๊ฒ€์ƒ‰ํ•ด๋ณด์•˜๊ณ , OUTS์˜ ANIMAL_ID๊ฐ€ IS NULL์ธ ๊ฑธ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค๋Š” ํžŒํŠธ๋ฅผ ์–ป์—ˆ๋‹ค.

๐Ÿ’ญ ์ง„์งœ๋กœ LEFT JOINํ•œ ํ›„ INS์™€ OUTS์˜ ANIMAL_ID๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ OUTS์˜ ๋ช‡ ๋ถ€๋ถ„์ด ๋น„์–ด ์žˆ์—ˆ๋‹ค!

๐Ÿ’ญ ๊ฒ€์ƒ‰ ์•ˆ ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค JOIN ๋‹ค ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ทธ ๋‚ ๊นŒ์ง€...๐Ÿ‘Š๐Ÿป

 

SELECT INS.NAME, INS.DATETIME FROM ANIMAL_INS AS INS 
LEFT JOIN ANIMAL_OUTS AS OUTS 
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE OUTS.ANIMAL_ID IS NULL
ORDER BY DATETIME LIMIT 3