๐Ÿ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(1)

1082 ๋‹จ์–ด WHEREmysqlasgroup byORDER BYquery

๋ฌธ์ œ ์„ค๋ช…

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

๋ณดํ˜ธ์†Œ์—์„œ๋Š” ๋ช‡ ์‹œ์— ์ž…์–‘์ด ๊ฐ€์žฅ ํ™œ๋ฐœํ•˜๊ฒŒ ์ผ์–ด๋‚˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค. 09:00๋ถ€ํ„ฐ 19:59๊นŒ์ง€, ๊ฐ ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ์ž…์–‘์ด ๋ช‡ ๊ฑด์ด๋‚˜ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์‹œ๊ฐ„๋Œ€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ
SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‚˜์˜ ์ฝ”๋“œ

SELECT HOUR(DATETIME) AS HOUR, COUNT(HOUR(DATETIME)) AS COUNT FROM ANIMAL_OUTS WHERE HOUR(DATETIME) >= 9 AND HOUR(DATETIME) <= 19 GROUP BY HOUR(DATETIME) ORDER BY HOUR ASC;

HOUR() ๋‚ด์žฅํ•จ์ˆ˜๋กœ DATETIME ์ž๋ฃŒํ˜•์˜ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๊ณ  AND ์—ฐ์‚ฐ์ž ์กฐ๊ฑด์„ ๋‹ฌ์•„ ๋‘๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋™์‹œ๋น„๊ตํ•˜์—ฌ 09:00๋ถ€ํ„ฐ 19:59๊นŒ์ง€ ๋ฐœ์ƒํ•œ ์ž…์–‘ ์‚ฌ๊ฑด์„ ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•˜์—ฌ ์กฐํšŒํ•˜๊ณ  ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์กฐํšŒํ•จ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ