DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 12์„ธ ์ดํ•˜์ธ ์—ฌ์ž ํ™˜์ž ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ (SQL) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 12์„ธ ์ดํ•˜์ธ ์—ฌ์ž ํ™˜์ž ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ (SQL)

gimewn 2022. 10. 28. 01:04

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/132201?language=mysql 

 

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

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

programmers.co.kr

 

โœ๏ธ NULL ๊ฐ’์ธ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋ฒ•์„ ์ƒˆ๋กœ ๋ฐฐ์› ๋‹ค!

 

1๏ธโƒฃ CASE๋ฌธ์œผ๋กœ ์กฐ๊ฑด ๊ฑธ์–ด ์ฒ˜๋ฆฌํ•˜๊ธฐ

SELECT PT_NAME, PT_NO, GEND_CD, AGE, 
CASE WHEN TLNO IS NULL THEN 'NONE' ELSE TLNO END TLNO FROM PATIENT 
WHERE AGE <= 12 AND GEND_CD = 'W' ORDER BY AGE DESC, PT_NAME;

 

2๏ธโƒฃ IFNULL ํ•จ์ˆ˜๋กœ NULL ๊ฐ’ ์ฒดํฌํ•˜๊ธฐ

- IFNULL(column, ๋Œ€์ฒด ๊ฐ’)

SELECT PT_NAME, PT_NO, GEND_CD, AGE, IFNULL(TLNO, 'NONE') AS TLNO FROM PATIENT 
WHERE AGE <= 12 AND GEND_CD = 'W' ORDER BY AGE DESC, PT_NAME;

 

3๏ธโƒฃ COALESCE ํ•จ์ˆ˜๋กœ NULL ๊ฐ’ ์ฒดํฌํ•˜๊ธฐ

- COALESCE(column๋ช…, ๋Œ€์ฒด ๊ฐ’) or COALESCE(column๋ช…, column๋ช…, column๋ช…...)

- ์ œ์‹œ๋œ ๊ฐ’๋“ค ์ค‘ NULL์ด ์•„๋‹Œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ์ถœ๋ ฅ

SELECT PT_NAME, PT_NO, GEND_CD, AGE, COALESCE(TLNO, 'NONE') AS TLNO FROM PATIENT 
WHERE AGE <= 12 AND GEND_CD = 'W' ORDER BY AGE DESC, PT_NAME;