mysql实现按天统计最近7天的数据示例,希望能够帮到大家!mysql实现按天统计最近7天的数据示例,希望能够帮到大家! $time = time(); $today = strtotime(date('Y-m-d', time()));//零点时间戳 $todayDate = date('Y-m-d', time()); //最近7天 $sevenDaysAgo = strtotime(date('Y-m-d', time())) - 7 * 86400; $sql = "SELECT t1.day, COUNT(t2.id) user_numFROM (SEL
$time = time();
$today = strtotime(date('Y-m-d', time()));//零点时间戳
$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,
id
FROM
user_log
WHERE create_time > $sevenDaysAgo
AND create_time < $time
) t2
ON t2.day = t1.day
GROUP BY t1.`day`";
沃梦达教程
本文标题为:MySQL实现按天统计最近七天数据
猜你喜欢
- SQL Server 2022 AlwaysOn新特性之包含可用性组详解 2023-07-29
- 搭建单机Redis缓存服务的实现 2023-07-13
- Oracle 删除大量表记录操作分析总结 2023-07-23
- 基于Python制作一个简单的文章搜索工具 2023-07-28
- MySQL8.0.28安装教程详细图解(windows 64位) 2023-07-26
- redis清除数据 2023-09-13
- 在阿里云CentOS 6.8上安装Redis 2023-09-12
- Numpy中如何创建矩阵并等间隔抽取数据 2023-07-28
- SQLSERVER调用C#的代码实现 2023-07-29
- Mongodb启动报错完美解决方案:about to fork child process,waiting until server is ready for connections. 2023-07-16