각 동물 col
의 이름은 각각 총 몇 개인지 구하기 (이름의 개수 구하기)
- 중복은 제거하는 DISTINCT 사용 필수!!
select count(DISTINCT NAME) as "count" from (ANIMAL_INS)
where NAME is not null
조건에 해당하는 col
기준으로 group by col
하기
select NAME, count(NAME) as 'count' from ANIMAL_INS
where NAME is not null
group by NAME
having count(NAME) >= 2
order by NAME ASC
null(값이 없는) col은 'No name'으로 표현하기
select ANIMAL_TYPE, ifnull(NAME, 'No name'), SEX_UPON_INTAKE
from (ANIMAL_INS)
주어진 글자를 포함하는 조건
select A.ANIMAL_ID, A.ANIMAL_TYPE, A.NAME from ANIMAL_INS as A, ANIMAL_OUTS as B
where A.ANIMAL_ID = B.ANIMAL_ID
and A.SEX_UPON_INTAKE like 'Intact%'
and B.SEX_UPON_OUTCOME not like 'Intact%'
order by A.ANIMAL_ID
col 값의 조건에 따라 다른 값으로 표현하기
- CASE WHEN / col이름 / 조건 / THEN / 변경할 값1 / ELSE / 변경할 값2 / END
select ANIMAL_ID, NAME, (CASE WHEN SEX_UPON_INTAKE like '%Neutered%' or SEX_UPON_INTAKE like '%Spayed%' THEN 'O' ELSE 'X' END) as '중성화'
from ANIMAL_INS
날짜 정보를 다른 포맷으로 바꾸기
- date_format() 함수 : 날짜를 지정해주는 포맷으로 변경 적용
select ANIMAL_ID, NAME, date_format(DATETIME, '%Y-%m-%d') as '날짜'
from ANIMAL_INS
❓ 잘 모르겠는 문제
select A.HOUR, ifnull(B.COUNT, 0) as 'COUNT' from(
select 0 as 'HOUR'
union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9
union select 10 union select 11 union select 12 union select 13
union select 14 union select 15 union select 16 union select 17 union select 18 union select 19
union select 20 union select 21 union select 22 union select 23
) as A
LEFT JOIN(
select hour(DATETIME) as 'HOUR', count(ANIMAL_ID) as 'COUNT' from ANIMAL_OUTS
group by HOUR
) as B
ON A.HOUR = B.HOUR
order by A.HOUR