" MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: ์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ (WHERE๋ฌธ)
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ˜„์—…๊ด‘๊ณ ์ธ/๊ฐ์ข…๊ฟ€ํŒ

MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: ์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ (WHERE๋ฌธ)

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

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ

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

programmers.co.kr

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

 


Q ๋ฌธ์ œ: ํŠน์ •์ปฌ๋Ÿผ์—์„œ ํŠน์ •๊ฐ’์„ ์ œ์™ธํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ 

A ๋ฌธ์ œํ’€์ด: 

-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS
WHERE INTAKE_CONDITION NOT IN ('Aged')
ORDER BY ANIMAL_ID ASC
-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS
WHERE INTAKE_CONDITION !='Aged'
ORDER BY ANIMAL_ID ASC
-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS
WHERE NOT INTAKE_CONDITION = 'Aged'
ORDER BY ANIMAL_ID ASC

 

 

ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ์กฐ๊ฑด๋ฌธ์ธ WHERE๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ์ •๋ ฌํ•  ์ˆ˜์žˆ๋Š”์ง€๋ฅผ

ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. 

 


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

ํŠน์ • ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜๋Š” ํ…Œ์ด๋ธ” ์ถœ๋ ฅ 

SELECT ์ถœ๋ ฅํ•˜๊ณ ์‹ถ์€์ปฌ๋Ÿผ๋ช… (์ „์ฒด๋Š” *) FROM ์ด์šฉ๋ฐ์ดํ„ฐ์…‹
WHERE ํ•ด๋‹น์ปฌ๋Ÿผ๋ช… = ํ•ด๋‹น๋ฐ์ดํ„ฐ

ํŠน์ • ์ปฌ๋Ÿผ ๊ธฐ์ค€ํ•˜์—ฌ ํ…Œ์ด๋ธ” ์ถœ๋ ฅ

SELECT * FROM ์ด์šฉ๋ฐ์ดํ„ฐ์…‹
# ์˜ค๋ฆ„์ฐจ์ˆœ
ORDER BY ๊ธฐ์ค€ ์ปฌ๋ ด๋ช… ASC 
# ๋‚ด๋ฆผ์ฐจ์ˆœ
ORDER BY ๊ธฐ์ค€ ์ปฌ๋Ÿผ๋ช… DESC

ASC (Ascend): ์˜ค๋ฅด๋‹ค

DESC (Descend): ๋‚ด๋ ค๊ฐ€๋‹ค 

๋ฐ˜์‘ํ˜•