0

获取一组日期之间的错过日期。我想在从日期到日期之间获取错过日期。我是 mysql 的新手,请帮助我获取日期之间的错过日期。

4

2 回答 2

0

这里有一个类似的步骤:获取两个日期之间的日期列表

于 2012-07-07T13:05:59.003 回答
0

这个答案是从上面引用的答案中巧妙地扯掉了paxdiablo答案

我所做的只是添加了一些测试数据和一个使用左外连接查找缺失日期的示例查询。我在您的评论中使用了测试数据,即

“假设我的表有 01-01-2012,01-02-2012,01-04-2012,01-05-2012 和 01-07-2012。现在我想得到 01-03-2012 和 01-06- 2012. 我怎么能得到这个”

你只需要找到一种填充all_dates表格的方法。这有点取决于什么构成了您的“全套日期”。未来的每个日期?未来每个月的第一天?过去和未来的每个日期?过去和未来每个月的第一天?在这里由你决定。我以 2012 年每个月的第一天为例:

create table all_dates
(id int unsigned not null primary key auto_increment,
a_date date not null,
unique key `all_dates_uidx1`(a_date));

-- First day of every month in 2012    
insert into all_dates (a_date) values ('2012-01-01');
insert into all_dates (a_date) values ('2012-02-01');
insert into all_dates (a_date) values ('2012-03-01');
insert into all_dates (a_date) values ('2012-04-01');
insert into all_dates (a_date) values ('2012-05-01');
insert into all_dates (a_date) values ('2012-06-01');
insert into all_dates (a_date) values ('2012-07-01');
insert into all_dates (a_date) values ('2012-08-01');
insert into all_dates (a_date) values ('2012-09-01');
insert into all_dates (a_date) values ('2012-10-01');
insert into all_dates (a_date) values ('2012-11-01');
insert into all_dates (a_date) values ('2012-12-01');

-- Note that my_dates table is missing 2012-03-01 and 2012-06-01
create table my_dates
(id int unsigned not null primary key auto_increment,
a_date date not null);

insert into my_dates (a_date) values ('2012-01-01');
insert into my_dates (a_date) values ('2012-02-01');
insert into my_dates (a_date) values ('2012-04-01');
insert into my_dates (a_date) values ('2012-05-01');
insert into my_dates (a_date) values ('2012-07-01');
insert into my_dates (a_date) values ('2012-08-01');
insert into my_dates (a_date) values ('2012-09-01');
insert into my_dates (a_date) values ('2012-10-01');
insert into my_dates (a_date) values ('2012-11-01');
insert into my_dates (a_date) values ('2012-12-01');

-- Get me the dates in all_dates that are not present in my_dates 
-- i.e. missing dates
select ad.a_date
from all_dates ad
left join my_dates md on ad.a_date = md.a_date
where md.a_date is null;
于 2012-07-07T14:39:53.577 回答