你没有说数据库是什么,所以我在 PostgreSQL 中做了一个例子:
create table a (
since date,
upto date
);
insert into a (since, upto) values ('2017-01-01', '2017-10-25');
create table b (
since date,
upto date
);
insert into b (since, upto) values ('2017-04-24', '2018-01-15');
insert into b (since, upto) values ('2017-06-01', '2018-01-15');
insert into b (since, upto) values ('2016-08-15', '2017-10-04');
select a.*, b.*
from a, b
where a.upto > b.since + interval '6 month' and a.since < b.since
or b.upto > a.since + interval '6 month' and b.since < a.since
结果:
since upto since upto
------------------------------------------------------------
2017-01-01 2017-10-25 2017-04-24 2018-01-15
2017-01-01 2017-10-25 2016-08-15 2017-10-04