" MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ˜„์—…๊ด‘๊ณ ์ธ/๊ฐ์ข…๊ฟ€ํŒ

MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

by ๋…๋”˜ 2021. 1. 14.
728x90
๋ฐ˜์‘ํ˜•

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””

programmers.co.kr

 

ํ•ด๋‹น ๊ธ€์€ Programmers ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต์„ ํ† ๋Œ€๋กœ MYSQL ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


Q ๋ฌธ์ œ: NULL ์ฒ˜๋ฆฌํ•˜๊ธฐํ•˜๊ธฐ

 ๋ฌธ์ œํ’€์ด 

-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT ANIMAL_TYPE, COALESCE(NAME,'No name') AS NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
ORDER BY ANIMAL_ID ASC 

ํ•ด๋‹น๋ฌธ์ œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ’์˜ NULL(๊ฒฐ์ธก์น˜)๋ฅผ ํŠน์ •๊ฐ’์œผ๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 


E ๋ช…๋ น์–ด ์ •๋ฆฌ :

SELECT COALESCE(์ปฌ๋Ÿผ๋ช…,'์น˜ํ™˜ํ•  ๊ฐ’') 
FROM ์ด์šฉํ•  ๋ฐ์ดํ„ฐ์…‹

 

MYSQL์— COALESCE๋Š” ์˜ค๋ผํด์— NVL๋ช…๋ น์–ด์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋ฉฐ

๋งŒ์•ฝ ๊ฐ’์ด NULL๊ฐ’์ด๋ฉด ํŠน์ • ๊ฐ’์œผ๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. 

๋ฐ˜์‘ํ˜•