mysql计入一个月,总计数达到一个月

时间:2017-02-02 19:50:08

标签: mysql

我想要每月的记录数和从该月开始的记录总数。

例如,

我有一个看起来像这样的表:

   renderer: function(sprite, config, rendererData, index) {
        var value = rendererData.value;
        var color;
        if (value >= 95) {
            color = "#0000ff";
        } else if (value < 85) {
            color = "#00ff00";
        } else {
            color = "#ff0000";
        }
        return {
            fill: color
        };

    }

如何选择和分组这些输出为:

#id,created#

1,'2016-01-01'

2,'2011-02-02'

3,'2011-02-09'

4,'2011-02-05'

5,'2011-03-07'

6,'2011-03-08'

非常感谢。

2 个答案:

答案 0 :(得分:1)

你走了:

SELECT DATE_FORMAT(`created`,'%M %Y') AS month, COUNT(*) AS count,
(SELECT count(*) FROM test WHERE MONTH(created) <= MONTH(t.created)) AS total
FROM test t
GROUP BY MONTH(created);

这里是SQL Fiddle

答案 1 :(得分:0)

使用单表读取:

SELECT 
    CONCAT(LEFT(MONTHNAME(dt), 3), ' ', YEAR(dt)) month,
    new,
    @total:=@total + new total
FROM
    (SELECT 
        created - INTERVAL DAY(created) - 1 DAY dt, COUNT(*) new
    FROM
        t
    GROUP BY created - INTERVAL DAY(created) - 1 DAY
    ORDER BY dt) t
        CROSS JOIN
    (SELECT @total:=0) t2

Demo