mysql按天,按周,按月,按季度,按年统计数据

Posted 懒惰虫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql按天,按周,按月,按季度,按年统计数据相关的知识,希望对你有一定的参考价值。


SELECT
count(id)
FROM
rd_track_info
WHERE
DATE(create_time) < DATE_SUB(CURDATE(), INTERVAL 7 DAY);

SELECT
count(id)
FROM
rd_track_info
WHERE
DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 7 DAY);

SELECT
count(id)
FROM
rd_track_info
WHERE
DATE(create_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY);

 

/*按天统计*/
SELECT
    count(id) countNum,
    DATE(create_time) createTime
FROM
    rd_track_info
GROUP BY
    DATE(create_time)
ORDER BY
    DATE(create_time) DESC;


/*按周统计*/
SELECT
    count(id) countNum,
    WEEK(create_time) createTime
FROM
    rd_track_info
GROUP BY
    WEEK(create_time)
ORDER BY
    WEEK(create_time) DESC;


/*按月统计*/
SELECT
    count(id) countNum,
    MONTH(create_time) createTime
FROM
    rd_track_info
GROUP BY
    MONTH(create_time)
ORDER BY
    MONTH(create_time) DESC;


/*按季度统计*/
SELECT
    count(id) countNum,
    QUARTER(create_time) createTime
FROM
    rd_track_info
GROUP BY
    QUARTER(create_time)
ORDER BY
    QUARTER(create_time) DESC;

/*按年统计*/
SELECT
    count(id) countNum,
    YEAR(create_time) createTime
FROM
    rd_track_info
GROUP BY
    YEAR(create_time)
ORDER BY
    YEAR(create_time) DESC;

 

以上是关于mysql按天,按周,按月,按季度,按年统计数据的主要内容,如果未能解决你的问题,请参考以下文章

用sql语句按周按月按季按年统计

mysql 数据分析如何实现日报周报月报和年报?

MySQL统计函数记录——按月按季度按日时间段统计

请问如何设置日历的重复项目呢

oracle表分区详解(按天按月按年等)

Mysql按周,按月,按日,按小时分组统计数据