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

      1. <legend id='fyjEh'><style id='fyjEh'><dir id='fyjEh'><q id='fyjEh'></q></dir></style></legend>
      2. 获取两个日期之间的日期列表

        时间:2023-08-20

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

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

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

                    <tbody id='jqhDQ'></tbody>

                  <tfoot id='jqhDQ'></tfoot>
                  本文介绍了获取两个日期之间的日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用标准的 mysql 函数可以编写一个查询,该查询将返回两个日期之间的天数列表.

                  Using standard mysql functions is there a way to write a query that will return a list of days between two dates.

                  例如,给定 2009-01-01 和 2009-01-13,它将返回一个带有值的单列表:

                  eg given 2009-01-01 and 2009-01-13 it would return a one column table with the values:

                   2009-01-01 
                   2009-01-02 
                   2009-01-03
                   2009-01-04 
                   2009-01-05
                   2009-01-06
                   2009-01-07
                   2009-01-08 
                   2009-01-09
                   2009-01-10
                   2009-01-11
                   2009-01-12
                   2009-01-13
                  

                  看来我还不清楚.我想生成这个列表.我将值存储在数据库中(按日期时间),但希望它们在左外部连接上聚合到上述日期列表(我希望从某些连接的右侧获得空值几天,并将处理此问题).

                  It appears I have not been clear. I want to GENERATE this list. I have values stored in the database (by datetime) but want them to be aggregated on a left outer join to a list of dates as above (I am expecting null from the right side of some of this join for some days and will handle this).

                  推荐答案

                  我会使用这个存储过程将您需要的间隔生成到名为 time_intervals 的临时表中,然后加入并聚合您的数据表使用临时 time_intervals 表.

                  I would use this stored procedure to generate the intervals you need into the temp table named time_intervals, then JOIN and aggregate your data table with the temp time_intervals table.

                  该过程可以生成您在其中指定的所有不同类型的间隔:

                  The procedure can generate intervals of all the different types you see specified in it:

                  call make_intervals('2009-01-01 00:00:00','2009-01-10 00:00:00',1,'DAY')
                  .
                  select * from time_intervals  
                  .
                  interval_start      interval_end        
                  ------------------- ------------------- 
                  2009-01-01 00:00:00 2009-01-01 23:59:59 
                  2009-01-02 00:00:00 2009-01-02 23:59:59 
                  2009-01-03 00:00:00 2009-01-03 23:59:59 
                  2009-01-04 00:00:00 2009-01-04 23:59:59 
                  2009-01-05 00:00:00 2009-01-05 23:59:59 
                  2009-01-06 00:00:00 2009-01-06 23:59:59 
                  2009-01-07 00:00:00 2009-01-07 23:59:59 
                  2009-01-08 00:00:00 2009-01-08 23:59:59 
                  2009-01-09 00:00:00 2009-01-09 23:59:59 
                  .
                  call make_intervals('2009-01-01 00:00:00','2009-01-01 02:00:00',10,'MINUTE')
                  . 
                  select * from time_intervals
                  .  
                  interval_start      interval_end        
                  ------------------- ------------------- 
                  2009-01-01 00:00:00 2009-01-01 00:09:59 
                  2009-01-01 00:10:00 2009-01-01 00:19:59 
                  2009-01-01 00:20:00 2009-01-01 00:29:59 
                  2009-01-01 00:30:00 2009-01-01 00:39:59 
                  2009-01-01 00:40:00 2009-01-01 00:49:59 
                  2009-01-01 00:50:00 2009-01-01 00:59:59 
                  2009-01-01 01:00:00 2009-01-01 01:09:59 
                  2009-01-01 01:10:00 2009-01-01 01:19:59 
                  2009-01-01 01:20:00 2009-01-01 01:29:59 
                  2009-01-01 01:30:00 2009-01-01 01:39:59 
                  2009-01-01 01:40:00 2009-01-01 01:49:59 
                  2009-01-01 01:50:00 2009-01-01 01:59:59 
                  .
                  I specified an interval_start and interval_end so you can aggregate the 
                  data timestamps with a "between interval_start and interval_end" type of JOIN.
                  .
                  Code for the proc:
                  .
                  -- drop procedure make_intervals
                  .
                  CREATE PROCEDURE make_intervals(startdate timestamp, enddate timestamp, intval integer, unitval varchar(10))
                  BEGIN
                  -- *************************************************************************
                  -- Procedure: make_intervals()
                  --    Author: Ron Savage
                  --      Date: 02/03/2009
                  --
                  -- Description:
                  -- This procedure creates a temporary table named time_intervals with the
                  -- interval_start and interval_end fields specifed from the startdate and
                  -- enddate arguments, at intervals of intval (unitval) size.
                  -- *************************************************************************
                     declare thisDate timestamp;
                     declare nextDate timestamp;
                     set thisDate = startdate;
                  
                     -- *************************************************************************
                     -- Drop / create the temp table
                     -- *************************************************************************
                     drop temporary table if exists time_intervals;
                     create temporary table if not exists time_intervals
                        (
                        interval_start timestamp,
                        interval_end timestamp
                        );
                  
                     -- *************************************************************************
                     -- Loop through the startdate adding each intval interval until enddate
                     -- *************************************************************************
                     repeat
                        select
                           case unitval
                              when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
                              when 'SECOND'      then timestampadd(SECOND, intval, thisDate)
                              when 'MINUTE'      then timestampadd(MINUTE, intval, thisDate)
                              when 'HOUR'        then timestampadd(HOUR, intval, thisDate)
                              when 'DAY'         then timestampadd(DAY, intval, thisDate)
                              when 'WEEK'        then timestampadd(WEEK, intval, thisDate)
                              when 'MONTH'       then timestampadd(MONTH, intval, thisDate)
                              when 'QUARTER'     then timestampadd(QUARTER, intval, thisDate)
                              when 'YEAR'        then timestampadd(YEAR, intval, thisDate)
                           end into nextDate;
                  
                        insert into time_intervals select thisDate, timestampadd(MICROSECOND, -1, nextDate);
                        set thisDate = nextDate;
                     until thisDate >= enddate
                     end repeat;
                  
                   END;
                  

                  这篇文章,我在那里为 SQL Server 构建了一个类似的函数.

                  Similar example data scenario at the bottom of this post, where I built a similar function for SQL Server.

                  这篇关于获取两个日期之间的日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在单个表中存储多个选项? 下一篇:如何转义用作列名的保留字?MySQL/创建表

                  相关文章

                  最新文章

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

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

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