sql 查询统计最近7天,30天,1年的数据

统计最近7天每天的数据

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天每天的数据

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;

统计最近一年每个月的数据

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;

你可能感兴趣的:(Mysql,sql,数据库)