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

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

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

    2. <tfoot id='PRH1S'></tfoot>

      <legend id='PRH1S'><style id='PRH1S'><dir id='PRH1S'><q id='PRH1S'></q></dir></style></legend>
      1. 在 sql 结果中填充空日期的最直接方法是什么(在

        时间:2023-08-18
        <legend id='GGp0b'><style id='GGp0b'><dir id='GGp0b'><q id='GGp0b'></q></dir></style></legend>

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

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

              <tfoot id='GGp0b'></tfoot>

                  <bdo id='GGp0b'></bdo><ul id='GGp0b'></ul>
                  本文介绍了在 sql 结果中填充空日期的最直接方法是什么(在 mysql 或 perl 端)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在从一个 mysql 表构建一个快速的 csv,查询如下:

                  I'm building a quick csv from a mysql table with a query like:

                  select DATE(date),count(date) from table group by DATE(date) order by date asc;
                  

                  然后将它们转储到 perl 文件中:

                  and just dumping them to a file in perl over a:

                  while(my($date,$sum) = $sth->fetchrow) {
                      print CSV "$date,$sum\n"
                  }
                  

                  虽然数据中存在日期间隔:

                  There are date gaps in the data, though:

                  | 2008-08-05 |           4 | 
                  | 2008-08-07 |          23 | 
                  

                  我想用零计数条目填充数据以填充缺失的天数,最后得到:

                  I would like to pad the data to fill in the missing days with zero-count entries to end up with:

                  | 2008-08-05 |           4 | 
                  | 2008-08-06 |           0 | 
                  | 2008-08-07 |          23 | 
                  

                  我用每月的天数和一些数学计算了一个非常尴尬(而且几乎肯定有问题)的解决方法,但是在 mysql 或 perl 方面必须有更简单的方法.

                  I slapped together a really awkward (and almost certainly buggy) workaround with an array of days-per-month and some math, but there has to be something more straightforward either on the mysql or perl side.

                  有什么天才的想法/为什么我这么愚蠢?

                  Any genius ideas/slaps in the face for why me am being so dumb?

                  由于以下几个原因,我最终使用了一个存储过程,该过程为相关日期范围生成了一个临时表:

                  I ended up going with a stored procedure which generated a temp table for the date range in question for a couple of reasons:

                  • 我知道每次要查找的日期范围
                  • 不幸的是,有问题的服务器不是我可以在 atm 上安装 perl 模块的服务器,而且它的状态已经陈旧到没有远程安装任何东西 Date::-y

                  perl Date/DateTime 迭代答案也很好,我希望我可以选择多个答案!

                  The perl Date/DateTime-iterating answers were also very good, I wish I could select multiple answers!

                  推荐答案

                  当你在服务器端需要类似的东西时,你通常会创建一个包含两个时间点之间所有可能日期的表,然后用左加入这个表查询结果.像这样:

                  When you need something like that on server side, you usually create a table which contains all possible dates between two points in time, and then left join this table with query results. Something like this:

                  create procedure sp1(d1 date, d2 date)
                    declare d datetime;
                  
                    create temporary table foo (d date not null);
                  
                    set d = d1
                    while d <= d2 do
                      insert into foo (d) values (d)
                      set d = date_add(d, interval 1 day)
                    end while
                  
                    select foo.d, count(date)
                    from foo left join table on foo.d = table.date
                    group by foo.d order by foo.d asc;
                  
                    drop temporary table foo;
                  end procedure
                  

                  在这种特殊情况下,最好在客户端进行一些检查,如果当前日期不是 previos+1,则添加一些附加字符串.

                  In this particular case it would be better to put a little check on the client side, if current date is not previos+1, put some addition strings.

                  这篇关于在 sql 结果中填充空日期的最直接方法是什么(在 mysql 或 perl 端)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 MySQL 数据库中存储 Emoji 字符 下一篇:如何使用 node.js 在 mySQL 中进行批量插入

                  相关文章

                  最新文章

                      <bdo id='IjbxQ'></bdo><ul id='IjbxQ'></ul>
                  1. <small id='IjbxQ'></small><noframes id='IjbxQ'>

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