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

MYSQL ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—ฐ์Šต: ๋™๋ช… ๋™๋ฌผ์ˆ˜ ์ฐพ๊ธฐ (GROUP BY, HAVING)

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

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ

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

programmers.co.kr

 

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


Q ๋ฌธ์ œ: ์ด๋ฆ„์ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ 

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

-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
SELECT NAME, COUNT(NAME)
FROM ANIMAL_INS
GROUP BY NAME
HAVING COUNT(NAME) > 1;

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

 


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

SELECT ์ถœ๋ ฅํ• ์ปฌ๋Ÿผ COUNT(ํ™•์ธํ•˜๊ณ ์‹ถ์€๋ฐ์ดํ„ฐ์ปฌ๋Ÿผ) AS ์ปฌ๋Ÿผ๋ช…
FROM ์ด์šฉ๋ฐ์ดํ„ฐ์…‹
GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ๋ช…
HAVING COUNT(NAME) > ์ˆซ์ž ; 

GROUP BY ๊ทธ๋ฃนํ™”์‹œํ‚ฌ ์ปฌ๋Ÿผ๋ช…

COUNT ํ™•์ธํ•  ์ปฌ๋Ÿผ๋ช…:

 

ํ™•์ธํ•˜๊ณ ์‹ถ์€ ROW(ํ–‰)์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 

 

๋ฐ˜์‘ํ˜•