MySQL如何填充范围内缺少的日期? [英] MySQL how to fill missing dates in range?

查看:96
本文介绍了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如何填充范围内缺少的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆