<legend id='ys8WK'><style id='ys8WK'><dir id='ys8WK'><q id='ys8WK'></q></dir></style></legend>

      • <bdo id='ys8WK'></bdo><ul id='ys8WK'></ul>

    1. <tfoot id='ys8WK'></tfoot>

      <small id='ys8WK'></small><noframes id='ys8WK'>

        <i id='ys8WK'><tr id='ys8WK'><dt id='ys8WK'><q id='ys8WK'><span id='ys8WK'><b id='ys8WK'><form id='ys8WK'><ins id='ys8WK'></ins><ul id='ys8WK'></ul><sub id='ys8WK'></sub></form><legend id='ys8WK'></legend><bdo id='ys8WK'><pre id='ys8WK'><center id='ys8WK'></center></pre></bdo></b><th id='ys8WK'></th></span></q></dt></tr></i><div id='ys8WK'><tfoot id='ys8WK'></tfoot><dl id='ys8WK'><fieldset id='ys8WK'></fieldset></dl></div>
      1. MySQL如何填充范围内缺少的日期?

        时间:2023-08-20
      2. <i id='6rXDn'><tr id='6rXDn'><dt id='6rXDn'><q id='6rXDn'><span id='6rXDn'><b id='6rXDn'><form id='6rXDn'><ins id='6rXDn'></ins><ul id='6rXDn'></ul><sub id='6rXDn'></sub></form><legend id='6rXDn'></legend><bdo id='6rXDn'><pre id='6rXDn'><center id='6rXDn'></center></pre></bdo></b><th id='6rXDn'></th></span></q></dt></tr></i><div id='6rXDn'><tfoot id='6rXDn'></tfoot><dl id='6rXDn'><fieldset id='6rXDn'></fieldset></dl></div>
        <tfoot id='6rXDn'></tfoot>
          <bdo id='6rXDn'></bdo><ul id='6rXDn'></ul>

          <small id='6rXDn'></small><noframes id='6rXDn'>

              <tbody id='6rXDn'></tbody>
            <legend id='6rXDn'><style id='6rXDn'><dir id='6rXDn'><q id='6rXDn'></q></dir></style></legend>

                1. 本文介绍了MySQL如何填充范围内缺少的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个包含 2 列、日期和分数的表格.它最多有 30 个条目,对于过去 30 天的每一天.

                  I have a table with 2 columns, date and score. It has at most 30 entries, for each of the last 30 days one.

                  date      score
                  -----------------
                  1.8.2010  19
                  2.8.2010  21
                  4.8.2010  14
                  7.8.2010  10
                  10.8.2010 14
                  

                  我的问题是缺少某些日期 - 我想看看:

                  My problem is that some dates are missing - I want to see:

                  date      score
                  -----------------
                  1.8.2010  19
                  2.8.2010  21
                  3.8.2010  0
                  4.8.2010  14
                  5.8.2010  0
                  6.8.2010  0
                  7.8.2010  10
                  ...
                  

                  我需要从单个查询中得到:19,21,9,14,0,0,10,0,0,14...这意味着缺少的日期用 0 填充.

                  What I need from the single query is to get: 19,21,9,14,0,0,10,0,0,14... That means that the missing dates are filled with 0.

                  我知道如何获取所有值并使用服务器端语言迭代日期并遗漏空格.但这是否可以在 mysql 中进行,以便我按日期对结果进行排序并获取丢失的部分.

                  I know how to get all the values and in server side language iterating through dates and missing the blanks. But is this possible to do in mysql, so that I sort the result by date and get the missing pieces.

                  在这个表中还有一个名为 UserID 的列,所以我有 30.000 个用户,其中一些在这个表中有分数.如果日期<,我每天都会删除日期30 天前,因为我需要每个用户最近 30 天的分数.原因是我正在制作过去 30 天的用户活动图表,并绘制图表我需要用逗号分隔的 30 个值.所以我可以说在查询中让我获得 USERID=10203 活动,查询将获得 30 个分数,过去 30 天的每一天都有一个分数.我希望我现在更清楚了.

                  In this table there is another column named UserID, so I have 30.000 users and some of them have the score in this table. I delete the dates every day if date < 30 days ago because I need last 30 days score for each user. The reason is I am making a graph of the user activity over the last 30 days and to plot a chart I need the 30 values separated by comma. So I can say in query get me the USERID=10203 activity and the query would get me the 30 scores, one for each of the last 30 days. I hope I am more clear now.

                  推荐答案

                  MySQL 没有递归功能,因此您只能使用 NUMBERS 表技巧 -

                  MySQL doesn't have recursive functionality, so you're left with using the NUMBERS table trick -

                  1. 创建一个只保存递增数字的表 - 使用 auto_increment 很容易做到:

                  1. Create a table that only holds incrementing numbers - easy to do using an auto_increment:

                  DROP TABLE IF EXISTS `example`.`numbers`;
                  CREATE TABLE  `example`.`numbers` (
                    `id` int(10) unsigned NOT NULL auto_increment,
                     PRIMARY KEY  (`id`)
                  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                  

                2. 使用以下方法填充表格:

                3. Populate the table using:

                  INSERT INTO `example`.`numbers`
                    ( `id` )
                  VALUES
                    ( NULL )
                  

                  ...根据您的需要获取尽可能多的值.

                  ...for as many values as you need.

                  使用DATE_ADD 构建日期列表,根据 NUMBERS.id 值增加天数.用您各自的开始和结束日期替换2010-06-06"和2010-06-14"(但使用相同的格式,YYYY-MM-DD)-

                  Use DATE_ADD to construct a list of dates, increasing the days based on the NUMBERS.id value. Replace "2010-06-06" and "2010-06-14" with your respective start and end dates (but use the same format, YYYY-MM-DD) -

                  SELECT `x`.*
                    FROM (SELECT DATE_ADD('2010-06-06', INTERVAL `n`.`id` - 1 DAY)
                            FROM `numbers` `n`
                           WHERE DATE_ADD('2010-06-06', INTERVAL `n`.`id` -1 DAY) <= '2010-06-14' ) x
                  

                4. LEFT JOIN 根据时间部分加入您的数据表:

                5. LEFT JOIN onto your table of data based on the time portion:

                     SELECT `x`.`ts` AS `timestamp`,
                            COALESCE(`y`.`score`, 0) AS `cnt`
                       FROM (SELECT DATE_FORMAT(DATE_ADD('2010-06-06', INTERVAL `n`.`id` - 1 DAY), '%m/%d/%Y') AS `ts`
                               FROM `numbers` `n`
                              WHERE DATE_ADD('2010-06-06', INTERVAL `n`.`id` - 1 DAY) <= '2010-06-14') x
                  LEFT JOIN TABLE `y` ON STR_TO_DATE(`y`.`date`, '%d.%m.%Y') = `x`.`ts`
                  

                6. 如果要保持日期格式,请使用 DATE_FORMAT 函数:

                  If you want to maintain the date format, use the DATE_FORMAT function:

                  DATE_FORMAT(`x`.`ts`, '%d.%m.%Y') AS `timestamp`
                  

                  这篇关于MySQL如何填充范围内缺少的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取每组分组SQL结果的最大值记录 下一篇:我应该在 MySQL 中使用日期时间还是时间戳数据类

                  相关文章

                  最新文章

                    <bdo id='tB7lM'></bdo><ul id='tB7lM'></ul>
                  <legend id='tB7lM'><style id='tB7lM'><dir id='tB7lM'><q id='tB7lM'></q></dir></style></legend>

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

                    1. <small id='tB7lM'></small><noframes id='tB7lM'>