mysql 求两日期_mysql – 获取两个日期之间的日期列表

我将使用此存储过程在名为time_intervals的临时表中生成所需的间隔,然后使用temp time_intervals表将JOIN和数据表聚合。

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

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;

类似的示例数据场景在底部this post,其中我为SQL Server构建了类似的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值