Code/Database (DB)
[SQL] 날짜별 레코드 카운트 구하기
codens
2020. 7. 31. 00:33
* 날짜별 자료의 갯수 합계를 한번에 구하기
SELECT id, DATE_FORMAT(created_at, '%Y-%m-%d') AS date1, COUNT(id)
FROM table
GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d')
ORDER BY created_at DESC LIMIT 1000;
//-----------------------------------------------------------------------------
* 정해진 기간내의 시간별 카운트
select count(*), created_at, logs .*
from logs
where created_at >= '2023-04-12' and created_at <= '2023-04-28'
group by hour(created_at);
//-----------------------------------------------------------------------------
< 참고 >
DateTime group by date and hour
https://stackoverflow.com/questions/14845981/datetime-group-by-date-and-hour
반응형