MySQL如何填充范围内缺少的日期?

MySQL how to fill missing dates in range?(MySQL如何填充范围内缺少的日期?)
本文介绍了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;

  • 使用以下方法填充表格:

  • 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
    

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

  • 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`
    

  • 如果要保持日期格式,请使用 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如何填充范围内缺少的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
    SQL query to group by day(按天分组的 SQL 查询)
    What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
    MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
    MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
    Include missing months in Group By query(在 Group By 查询中包含缺失的月份)