본문 바로가기

Programming/SQL

[MYSQL] SELECT groupby FROM Programmers

 

 

 

문제정보

 

 

SELECT ANIMAL_TYPE, COUNT(ANIMAL_ID) AS count FROM ANIMAL_INS GROUP BY ANIMAL_TYPE;

 

 

SELECT NAME, COUNT(ANIMAL_ID) AS COUNT FROM ANIMAL_INS WHERE NAME IS NOT NULL
GROUP BY NAME HAVING COUNT>=2 ORDER BY NAME;

HAVING

- GROUP의 조건

 

SELECT HOUR(DATETIME) AS HOUR, COUNT(ANIMAL_ID) AS COUNT FROM ANIMAL_OUTS
GROUP BY HOUR HAVING HOUR >=9 AND HOUR <20 ORDER BY HOUR;

 

 

SET @hour := -1;
SELECT (@hour := @hour+1) AS HOUR,
(SELECT COUNT(ANIMAL_ID) FROM ANIMAL_OUTS WHERE HOUR(DATETIME) = @hour)
FROM ANIMAL_OUTS WHERE @hour < 23 ORDER BY HOUR;

SET

- 변수 등을 세팅할 수 있음

- @hour := -1 -> hour이라는 변수를 -1로 초기화