개발 꿀팁/PHP

MySQL 날짜별 최근 7일 데이터 집계

Jammie 2022. 11. 1. 17:33
반응형
$time = time( );
$today = strtotime(date('Y-m-d', time()); // 시간 스탬프 0
$todayDate = date('Y-m-d', time( ));
//최근 7일
$sevenDaysAgo = strtotime(date('Y-m-d', time()) - 7 * 86400;
$sql = "SELECT
t1.day,
COUNT(t2.id) user_num
FROM
(SELECT)
@cdate:= DATE_ADD(@cdate, INTERVAL - 1 DAY) day
FROM
(SELECT)
@cdate:= DATE_ADD('{$todayDate}', INTERVAL + 1 DAY)
FROM
area) t0
LIMIT 7) t1
LEFT JOIN
(SELECT)
FROM_UNIXTIME(create_time, '%Y-%m-%d') day,
아이디
FROM
user_log
WHERE create_time > $sevenDaysAgo
AND create_time < $time
) t2
On t2.day = t1.day
GROUP BY t1. `day`;
반응형