这听起来像你正在寻找的SQL overlaps
运营商,我认为MySQL不支持。所述overlaps
操作者被定义为
row_constructor overlaps row_constructor
和行构造具有形式
(Ls, Le) OVERLAPS (Rs, Re)
其中LS装置左手启动,勒装置左手端,等等
overlaps
运营商是defi ned在语义上等同于这个表达式。
(Ls > Rs and (Ls < Re or Le < Re)) or
(Rs > Ls and (Rs < Le or Re < Le)) or
(Ls = Rs and Le is not null and Re is not null)
替换你的值给了我这样的东西。 (未测试)
select *
from myTable
where ('2002-12-29' > fec_inicio and ('2002-12-29' < fec_fin or '2002-12-30' < fec_fin)) or
(fec_inicio > '2002-12-29' and (fec_inicio < '2002-12-30' or fec_fin < '2002-12-30')) or
('2002-12-29' = fec_inicio and '2002-12-30' is not null and fec_fin is not null);
我希望我能得到那些替代权。 (并且我们知道希望不会很好地扩展。)
从您的问题中不清楚您需要如何处理重叠多行的范围。但是,例如,应该很简单,从上面的查询中选择最短日期,并在该日期选择inner join
以获取完整行。
说了这么多,你选择百分比的逻辑看起来很奇怪。给它一些想法。
您是否尝试过使用BETWEEN运算符? –
如果你有2002-12-29和2003-10-25你想看到多少百分比?一些如何逻辑似乎不正确。你不应该只有一个日期来比较,并得到该日期的百分比? – Stephen
仍在挣扎?见http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry