统计最近7天每天的数据
第一个查询统计了最近7天内,每天创建的’WAIT_SEND’状态的订单数量。这是通过将当前日期向前推移6天(DATE_SUB(CURDATE(), INTERVAL 6 DAY))实现的。然后,它会按照创建日期对结果进行分组(GROUP BY createdTime),并统计每个日期中的订单数量。
SELECT DATE(created_time) AS createdTime, COUNT(merchandise_count) AS count
FROM t_wellness_order
WHERE 1=1
AND created_time >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
AND t_wellness_order.state = 'WAIT_SEND'
GROUP BY createdTime
ORDER BY createdTime ASC;
统计最近30天每天的数据
第二个查询与第一个查询类似,但是它统计的是最近30天内,每天创建的’WAIT_SEND’状态的订单数量。这是通过将当前日期向前推移29天(DATE_SUB(CURDATE(), INTERVAL 29 DAY))实现的。
SELECT DATE(created_time) AS createdTime, COUNT(merchandise_count) AS count
FROM t_wellness_order
WHERE 1=1
AND created_time >= DATE_SUB(CURDATE(), INTERVAL 29 DAY)
AND t_wellness_order.state = 'WAIT_SEND'
GROUP BY createdTime
ORDER BY createdTime ASC;
统计最近一年每个月的数据
第三个查询与前两个不同,它统计的是最近一年内,每个月创建的’WAIT_SEND’状态的订单数量。这是通过将当前日期向前推移1年(DATE_SUB(NOW(), INTERVAL 1 YEAR))实现的。然后,它会将创建日期格式化为’年-月’(DATE_FORMAT(created_time, ‘%Y-%m’)),并按照这个格式对结果进行分组(GROUP BY createdTime),然后统计每个月的订单数量。
SELECT
DATE_FORMAT(created_time, '%Y-%m') AS createdTime,
COUNT(merchandise_count) AS count
FROM
t_wellness_order
WHERE
1=1
AND created_time >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
AND t_wellness_order.state = 'WAIT_SEND'
GROUP BY
createdTime
ORDER BY
createdTime;