• <legend id='HTaIO'><style id='HTaIO'><dir id='HTaIO'><q id='HTaIO'></q></dir></style></legend>
    1. <small id='HTaIO'></small><noframes id='HTaIO'>

        <bdo id='HTaIO'></bdo><ul id='HTaIO'></ul>

        <i id='HTaIO'><tr id='HTaIO'><dt id='HTaIO'><q id='HTaIO'><span id='HTaIO'><b id='HTaIO'><form id='HTaIO'><ins id='HTaIO'></ins><ul id='HTaIO'></ul><sub id='HTaIO'></sub></form><legend id='HTaIO'></legend><bdo id='HTaIO'><pre id='HTaIO'><center id='HTaIO'></center></pre></bdo></b><th id='HTaIO'></th></span></q></dt></tr></i><div id='HTaIO'><tfoot id='HTaIO'></tfoot><dl id='HTaIO'><fieldset id='HTaIO'></fieldset></dl></div>
      1. <tfoot id='HTaIO'></tfoot>

        Mysql查询以获取每月计数

        时间:2024-04-16
          <bdo id='oAhf5'></bdo><ul id='oAhf5'></ul>

            <tbody id='oAhf5'></tbody>

            <legend id='oAhf5'><style id='oAhf5'><dir id='oAhf5'><q id='oAhf5'></q></dir></style></legend>
                • <small id='oAhf5'></small><noframes id='oAhf5'>

                • <i id='oAhf5'><tr id='oAhf5'><dt id='oAhf5'><q id='oAhf5'><span id='oAhf5'><b id='oAhf5'><form id='oAhf5'><ins id='oAhf5'></ins><ul id='oAhf5'></ul><sub id='oAhf5'></sub></form><legend id='oAhf5'></legend><bdo id='oAhf5'><pre id='oAhf5'><center id='oAhf5'></center></pre></bdo></b><th id='oAhf5'></th></span></q></dt></tr></i><div id='oAhf5'><tfoot id='oAhf5'></tfoot><dl id='oAhf5'><fieldset id='oAhf5'></fieldset></dl></div>
                  <tfoot id='oAhf5'></tfoot>
                • 本文介绍了Mysql查询以获取每月计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的用户有一个表,其中有一个名为created"的字段,其中包含注册日期.

                  I have a table for my users that have a field named "created" that have the registration date.

                  如何获取包含过去 12 个月内每月注册数量计数的列表?像这样:

                  How can i get a list that contains a count for the registrations number per month in last 12 months? Like this:

                  Month   Count
                  1        1232
                  2        2222
                  3         122
                  4        4653
                  ...       ...
                  12       7654
                  

                  我不习惯使用 mysql,所以直到现在我只知道如何计算去年的注册数量,而不是如何按过去 12 个月对计数进行分组.提前致谢!

                  I'm not used to working with mysql, so until now i just know how to count the number of registrations in last year, not how to group that count by last 12 months. Thanks in advance!

                  更新

                  现在我得到了这个,使用@fthiella 解决方案:

                  Now I'm getting this, using @fthiella solution:

                  +------------------------------+-------------------------------+----------+
                  | Year(FROM_UNIXTIME(created)) | Month(FROM_UNIXTIME(created)) | Count(*) |
                  +------------------------------+-------------------------------+----------+
                  |                         2012 |                             4 |     9927 |
                  |                         2012 |                             5 |     5595 |
                  |                         2012 |                             6 |     4431 |
                  |                         2012 |                             7 |     3299 |
                  |                         2012 |                             8 |      429 |
                  |                         2012 |                            10 |     3698 |
                  |                         2012 |                            11 |     6208 |
                  |                         2012 |                            12 |     5142 |
                  |                         2013 |                             1 |     1196 |
                  |                         2013 |                             2 |       10 |
                  +------------------------------+-------------------------------+----------+
                  

                  如何强制查询为我提供 count = 0 的月份?

                  How can i force query to give me the months with count = 0?

                  @fthiella 的解决方案(非常感谢!):

                  Solution by @fthiella (thanks a lot!):

                       SELECT y, m, Count(users.created)
                       FROM (
                        SELECT y, m
                        FROM
                           (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
                           (SELECT 1 m UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
                             UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
                             UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months) ym
                         LEFT JOIN users
                         ON ym.y = YEAR(FROM_UNIXTIME(users.created))
                            AND ym.m = MONTH(FROM_UNIXTIME(users.created))
                       WHERE
                         (y=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
                         OR
                         (y<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
                       GROUP BY y, m;
                  

                  结果:

                  +------+----+----------------------+
                  | y    | m  | Count(users.created) |
                  +------+----+----------------------+
                  | 2012 |  5 |                 5595 |
                  | 2012 |  6 |                 4431 |
                  | 2012 |  7 |                 3299 |
                  | 2012 |  8 |                  429 |
                  | 2012 |  9 |                    0 |
                  | 2012 | 10 |                 3698 |
                  | 2012 | 11 |                 6208 |
                  | 2012 | 12 |                 5142 |
                  | 2013 |  1 |                 1196 |
                  | 2013 |  2 |                   10 |
                  | 2013 |  3 |                    0 |
                  | 2013 |  4 |                    0 |
                  +------+----+----------------------+
                  

                  推荐答案

                  如果 created 是一个 INT 字段,你应该使用 FROM_UNIXTIME 函数将其转换为日期字段,然后 MONTH 提取月份的函数:

                  If created is an INT field, you should use FROM_UNIXTIME function to convert it to a date field, and then MONTH function to extract the month:

                  SELECT Month(FROM_UNIXTIME(created)), Count(*)
                  FROM yourtable
                  WHERE FROM_UNIXTIME(created) >= CURDATE() - INTERVAL 1 YEAR
                  GROUP BY Month(FROM_UNIXTIME(created))
                  

                  这将计算过去 12 个月内创建的所有行.请注意,最好也按年份分组:

                  this will count all the rows that have been created in the last 12 months. Please notice that it's probably better to also group by the YEAR:

                  SELECT Year(FROM_UNIXTIME(created)), Month(FROM_UNIXTIME(created)), Count(*)
                  FROM yourtable
                  WHERE FROM_UNIXTIME(created) >= CURDATE() - INTERVAL 1 YEAR
                  GROUP BY Year(FROM_UNIXTIME(created)), Month(FROM_UNIXTIME(created))
                  

                  如果您需要计算注册号而不是行数,您可以使用类似

                  If you need to count the registration numbers instead of the rows, you could use something like

                  COUNT(registration_number)
                  

                  跳过空值,或

                  COUNT(DISTINCT registration_number)
                  

                  只计算不同的.

                  编辑

                  如果您还需要显示 count=0 的月份,我会使用这样的查询来返回当前和上一年的所有月份:

                  If you also need to show months that have count=0, I would use a query like this that returns all of the months for the current and for the previous year:

                  SELECT y, m
                  FROM
                    (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
                    (SELECT 1 m UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
                      UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
                      UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months;
                  

                  然后我会使用一个LEFT JOIN,它返回第一个查询的所有行,并且只返回匹配的第二个查询的行:

                  And then I'd use a LEFT JOIN, that returns all of the rows of the first query, and only the rows of the second query that matches:

                  SELECT y, m, Count(yourtable.created)
                  FROM (
                    SELECT y, m
                    FROM
                      (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
                      (SELECT 1 m UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
                        UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
                        UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months) ym
                    LEFT JOIN yourtable
                    ON ym.y = YEAR(FROM_UNIXTIME(yourtable.created))
                       AND ym.m = MONTH(FROM_UNIXTIME(yourtable.created))
                  WHERE
                    (y=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
                    OR
                    (y<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
                  GROUP BY y, m
                  

                  (请注意,这里我只考虑过去 12 个月,所以如果我们在 2013 年 4 月中旬,它将计算 2012 年 5 月 - 4 月 13 日之间的行数,如果这不是正确的行为,请告诉我)

                  (please notice that here I am considering just the last 12 months, so if we are in the middle April 2013 it will count rows in the interval May 2012 - April 13, if this is not the correct behaviour please let me know)

                  这篇关于Mysql查询以获取每月计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何按计算字段分组 下一篇:在 MySQL 中使用 GROUP BY 选择最近的行

                  相关文章

                    <i id='yxE6C'><tr id='yxE6C'><dt id='yxE6C'><q id='yxE6C'><span id='yxE6C'><b id='yxE6C'><form id='yxE6C'><ins id='yxE6C'></ins><ul id='yxE6C'></ul><sub id='yxE6C'></sub></form><legend id='yxE6C'></legend><bdo id='yxE6C'><pre id='yxE6C'><center id='yxE6C'></center></pre></bdo></b><th id='yxE6C'></th></span></q></dt></tr></i><div id='yxE6C'><tfoot id='yxE6C'></tfoot><dl id='yxE6C'><fieldset id='yxE6C'></fieldset></dl></div>

                    <tfoot id='yxE6C'></tfoot>
                    • <bdo id='yxE6C'></bdo><ul id='yxE6C'></ul>
                    <legend id='yxE6C'><style id='yxE6C'><dir id='yxE6C'><q id='yxE6C'></q></dir></style></legend>
                  1. <small id='yxE6C'></small><noframes id='yxE6C'>