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实现按天统计最近七天数据
基础教程推荐
猜你喜欢
- 【Redis】数据持久化 2023-09-12
- Python常见库matplotlib学习笔记之多个子图绘图 2023-07-27
- Mysql查询所有表和字段信息的方法 2023-07-26
- Mysql主从三种复制模式(异步复制,半同步复制,组复 2022-09-01
- Sql Server Management Studio连接Mysql的实现步骤 2023-07-29
- Redis如何实现延迟队列 2023-07-13
- 如何将excel表格数据导入postgresql数据库 2023-07-20
- SQLServer 清理日志的实现 2023-07-29
- 关于MySQL中explain工具的使用 2023-07-27
- python中pandas库的iloc函数用法解析 2023-07-28