• <small id='nQElv'></small><noframes id='nQElv'>

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

  • <tfoot id='nQElv'></tfoot>

        <bdo id='nQElv'></bdo><ul id='nQElv'></ul>
      1. MySQL - 填充缺失的日期

        时间:2023-09-25
        1. <legend id='hIeON'><style id='hIeON'><dir id='hIeON'><q id='hIeON'></q></dir></style></legend>

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

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

            • <bdo id='hIeON'></bdo><ul id='hIeON'></ul>
                  <tfoot id='hIeON'></tfoot>
                    <tbody id='hIeON'></tbody>

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

                  问题描述

                  我有这个查询,我想用一些值填充缺失的日期(例如零...)

                  I have this query and I want to fill missing dates with some values (zero for example...)

                  SELECT date, SUM(val1) as sum_val  
                  FROM table1  
                  WHERE date BETWEEN '2016-03-04' AND '2016-03-10'  
                  GROUP BY date
                  

                  结果如下:

                  |--------------------------  
                  |date, sum_val  
                  |--------------------------  
                  |2016-03-04, 21568  
                  |--------------------------  
                  |2016-03-05, 14789  
                  |--------------------------  
                  |2016-03-08, 20568  
                  |--------------------------  
                  |2016-03-10, 5841  
                  |--------------------------
                  

                  如何用零值填充缺失的日期?有没有人有想法?

                  How can I populate missing dates with zero values? Does anyone has an idea?

                  我需要这些数据用于<​​strong>图表预览.

                  I need this data for chart preview.

                  推荐答案

                  一般情况下,你可以在 MySQL 中使用以下方法生成一系列 N 个整数:

                  In general, you can generate a series of N integers in MySQL using:

                      select (@i:=@i+1)-1 as `myval` from someTable,(SELECT @i:=0) gen_sub limit N
                  

                  请注意,您加入的表 (someTable) 必须至少有 N 行. 上面的 -1 是使其基数为零...删除它,您将得到 1,2,3 对于 N=3.

                  Note that the table that you join on (someTable) must have at least N rows. The -1 above is to make it base-zero... remove it and you'll get 1,2,3 for N=3.

                  您可以将这些整数输入 DATE_ADD 函数以将其转换为一系列日期.为了便于理解,让我们为日期使用一些用户变量.

                  You can feed those integers into the DATE_ADD function to turn it into a series of dates. To make it easier to follow, let's use some user variables for the dates.

                  SET @date_min = '2016-03-04';
                  SET @date_max = '2016-03-10';
                  
                  select DATE_ADD(@date_min, INTERVAL (@i:=@i+1)-1 DAY) as `date`
                  from information_schema.columns,(SELECT @i:=0) gen_sub
                  where DATE_ADD(@date_min,INTERVAL @i DAY) BETWEEN @date_min AND @date_max
                  

                  这将返回这些天以及它们之间的每一天的行.现在只是加入你的表的问题......我没有尝试过,因为我没有你的数据库结构,但像下面这样的东西应该可以工作:

                  That will return rows for those days and every day between them. Now it's just a matter of joining against your table... I haven't tried it since I don't have your db structure, but something like the following should work:

                  SET @date_min = '2016-03-04';
                  SET @date_max = '2016-03-10';
                  
                  SELECT
                     date_generator.date,
                     ifnull(SUM(val1),0) as sum_val
                  from (
                     select DATE_ADD(@date_min, INTERVAL (@i:=@i+1)-1 DAY) as `date`
                     from information_schema.columns,(SELECT @i:=0) gen_sub 
                     where DATE_ADD(@date_min,INTERVAL @i DAY) BETWEEN @date_min AND @date_max
                  ) date_generator
                  left join table1 on table1.date = date_generator.date
                  GROUP BY date;
                  

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

                  上一篇:将 Google 图表另存为图像 下一篇:谷歌图表错误:b.L 不是函数

                  相关文章

                  最新文章

                  <small id='1Lh6E'></small><noframes id='1Lh6E'>

                  <tfoot id='1Lh6E'></tfoot>

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