DevYoon
[ํ๋ก๊ทธ๋๋จธ์ค] 12์ธ ์ดํ์ธ ์ฌ์ ํ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ (SQL) ๋ณธ๋ฌธ
PS/Programmers
[ํ๋ก๊ทธ๋๋จธ์ค] 12์ธ ์ดํ์ธ ์ฌ์ ํ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ (SQL)
gimewn 2022. 10. 28. 01:04link ๐ https://school.programmers.co.kr/learn/courses/30/lessons/132201?language=mysql
โ๏ธ 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;