mysql 每隔五分钟/五小时/五天/五月/两年统计一次记录等

每隔五分钟

SELECT concat( date_format(create_time, '%Y-%m-%d %H:' ) , floor( date_format(create_time, '%i' ) /5 ) ) AS 时间段, count( * ) as 数量
FROM 表名
GROUP BY 时间段

在这里插入图片描述

每隔五小时

SELECT concat( date_format(create_time, '%Y-%m-%d:' ) , floor( date_format(create_time, '%H' ) /5 ) ) AS 时间段, count( * ) as 数量
FROM user
GROUP BY 时间段

在这里插入图片描述

每隔五天

SELECT concat( date_format(create_time, '%Y-%m-%d,' ) , floor( date_format(create_time, '%d' ) /5 ) ) AS 时间段, count( * ) as 数量
FROM user
GROUP BY 时间段

每隔五月

SELECT concat( date_format(create_time, '%Y-%m,' ) , floor( date_format(create_time, '%m' ) /5 ) ) AS 时间段, count( * ) as 数量
FROM user
GROUP BY 时间段

在这里插入图片描述

每隔两年

SELECT concat( date_format(create_time, '%Y,' ) , floor( date_format(create_time, '%Y' ) /2 ) ) AS 时间段, count( * ) as 数量
FROM user
GROUP BY 时间段
发布了62 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40618664/article/details/105136756