" MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ (1)
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ˜„์—…๊ด‘๊ณ ์ธ/๊ฐ์ข…๊ฟ€ํŒ

MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ (1)

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

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(1)

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

programmers.co.kr

 

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


Q ๋ฌธ์ œ:  ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ (1) 

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

-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT HOUR(DATETIME) AS HOUR, COUNT(HOUR(DATETIME)) AS COUNT
FROM ANIMAL_OUTS
GROUP BY HOUR
HAVING HOUR
BETWEEN '9' AND '19'
ORDER BY HOUR ASC 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ํŠน์ • ์นผ๋Ÿผ์— ํŠน์ •๋ฒ”์œ„์•ˆ์—์„œ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ค‘๋ณต๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 


E: ์ฐธ๊ณ  ์ง€์‹

 

WHERE๊ณผ HAVING ์ ˆ์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”?

 

์ผ๋‹จ WHERE๊ณผ HAVING์ ˆ ๋ชจ๋‘ SQL QUERY์—์„œ ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ GROUP BY ์ ˆ์„ ํ•จ๊ป˜์“ธ ๋•Œ ์ž…๋‹ˆ๋‹ค. 

WHERE์ ˆ์— ๊ฐ™์€ ๊ฒฝ์šฐ, ๊ทธ๋ฃนํ™”๋ฅผ ํ•˜๊ธฐ์ „ ์กฐ๊ฑด์‹์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ HAVING์ ˆ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 

WHERE๋ฌธ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๊ทธ๋ฃนํ™” ํ›„ ์กฐ๊ฑด์‹์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 

 

 

๋ฐ˜์‘ํ˜•