mysql间隔日期,显示MySQL中日期之间的间隔

How can I show the remaining/complementary dates in mysql?

For example in my table that has 2 columns, a snapshot for

from_date>14-06-2014 and to_date<01-07-2014

would give this output:

From date || To_date

15-06-2014 || 20-06-2014

23-06-2014 || 27-06-2014

29-06-2014 || 30-06-2014

I would like to be able to show the dates that we have a gap and no records exist, like this:

2 //21-06-2014 - 23-06-2014

1 //28-06-2014

Is this possible?

Thank you

解决方案

along those lines:

drop table if exists dates;

create table dates (d_from date, d_to date);

insert into dates values

('2014-06-15' , '2014-06-20'),

('2014-06-23' , '2014-06-27' ),

('2014-06-29' , '2014-06-30' );

select low.d_to, high.d_from, to_days(high.d_from) - to_days(low.d_to) - 1 as gap

from dates low, dates high

where high.d_from = (select min(d_from) from dates where d_from > low.d_to)

;

Which means: join the table to itself on adjacent end/start dates and compute the difference.

+------------+------------+------+

| d_to | d_from | gap |

+------------+------------+------+

| 2014-06-20 | 2014-06-23 | 2 |

| 2014-06-27 | 2014-06-29 | 1 |

+------------+------------+------+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值