SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS `date`, COUNT(*) AS `count` FROM `table_name` WHERE created_at >= '2022-09-01 00:00:00' AND created_at<= '2022-09-30 23:59:59' GROUP BY `date`;
以上代码是一个基本的按天分组查询的例子。大家使用了MySQL的DATE_FORMAT函数来将时间戳格式化为日期,并使用GROUP BY语句以日期为分组条件进行统计。
如果大家需要按周、月、季度、年等时间段进行分组,可以使用相应的函数进行格式化,例如:
-- 按周分组查询 SELECT CONCAT(YEAR(created_at), '-', WEEK(created_at)) AS `week`, COUNT(*) AS `count` FROM `table_name` WHERE created_at >= '2022-09-01 00:00:00' AND created_at= '2022-09-01 00:00:00' AND created_at= '2022-01-01 00:00:00' AND created_at= '2022-01-01 00:00:00' AND created_at<= '2022-12-31 23:59:59' GROUP BY `year`;
以上就是基本的按时间段分组查询的示例代码。在实际的应用中,可以根据具体的需求进行修改和优化。