123

在 MySQL 中,如果我有一个日期范围列表(范围开始和范围结束)。例如

10/06/1983 to 14/06/1983
15/07/1983 to 16/07/1983
18/07/1983 to 18/07/1983

我想检查另一个日期范围是否包含列表中已有的任何范围,我该怎么做?

例如

06/06/1983 to 18/06/1983 = IN LIST
10/06/1983 to 11/06/1983 = IN LIST
14/07/1983 to 14/07/1983 = NOT IN LIST
4

10 回答 10

465

这是一个经典问题,如果你颠倒逻辑,它实际上更容易。

让我给你举个例子。

我将在这里发布一段时期,以及以某种方式重叠的其他时期的所有不同变化。

           |-------------------|          compare to this one
               |---------|                contained within
           |----------|                   contained within, equal start
                   |-----------|          contained within, equal end
           |-------------------|          contained within, equal start+end
     |------------|                       not fully contained, overlaps start
                   |---------------|      not fully contained, overlaps end
     |-------------------------|          overlaps start, bigger
           |-----------------------|      overlaps end, bigger
     |------------------------------|     overlaps entire period

另一方面,让我发布所有不重叠的内容:

           |-------------------|          compare to this one
     |---|                                ends before
                                 |---|    starts after

因此,如果您简单地将比较简化为:

starts after end
ends before start

然后你会找到所有不重叠的,然后你会找到所有不匹配的时期。

对于您最后的 NOT IN LIST 示例,您可以看到它符合这两个规则。

您需要确定以下时段是在您的范围内还是在您的范围之外:

           |-------------|
   |-------|                       equal end with start of comparison period
                         |-----|   equal start with end of comparison period

如果您的表有名为 range_end 和 range_start 的列,这里有一些简单的 SQL 来检索所有匹配的行:

SELECT *
FROM periods
WHERE NOT (range_start > @check_period_end
           OR range_end < @check_period_start)

注意里面的NOT。由于这两个简单的规则找到了所有不匹配的行,一个简单的 NOT 会反转它说:如果它不是不匹配的行之一,它必须是匹配的行之一

在这里应用简单的反转逻辑来摆脱 NOT,你最终会得到:

SELECT *
FROM periods
WHERE range_start <= @check_period_end
      AND range_end >= @check_period_start
于 2008-09-27T12:38:15.530 回答
8

以 06/06/1983 到 18/06/1983 的示例范围为例,假设您的范围有名为startend的列,您可以使用这样的子句

where ('1983-06-06' <= end) and ('1983-06-18' >= start)

即检查您的测试范围的开始是在数据库范围的结束之前,并且您的测试范围的结束是在数据库范围的开始之后或之后。

于 2008-09-27T12:41:20.593 回答
5

如果您的 RDBMS 支持 OVERLAP() 函数,那么这将变得微不足道——不需要本土解决方案。(在 Oracle 中它显然可以工作,但没有记录)。

于 2008-09-28T19:56:13.130 回答
0

在您的预期结果中,您说

06/06/1983 至 18/06/1983 = 在列表中

但是,此期间不包含也不包含在您的期间表(不是列表!)中的任何期间。但是,它确实与 1983 年 6 月 10 日至 1983 年 6 月 14 日期间重叠。

您可能会发现 Snodgrass 书 ( http://www.cs.arizona.edu/people/rts/tdbbook.pdf ) 很有用:它早于 mysql,但时间的概念没有改变;-)

于 2008-09-29T13:47:03.650 回答
0

我在 MySQL 中创建了处理这个问题的函数。只需在使用前将日期转换为秒。

DELIMITER ;;

CREATE FUNCTION overlap_interval(x INT,y INT,a INT,b INT)
RETURNS INTEGER DETERMINISTIC
BEGIN
DECLARE
    overlap_amount INTEGER;
    IF (((x <= a) AND (a < y)) OR ((x < b) AND (b <= y)) OR (a < x AND y < b)) THEN
        IF (x < a) THEN
            IF (y < b) THEN
                SET overlap_amount = y - a;
            ELSE
                SET overlap_amount = b - a;
            END IF;
        ELSE
            IF (y < b) THEN
                SET overlap_amount = y - x;
            ELSE
                SET overlap_amount = b - x;
            END IF;
        END IF;
    ELSE
        SET overlap_amount = 0;
    END IF;
    RETURN overlap_amount;
END ;;

DELIMITER ;
于 2011-05-03T03:24:04.330 回答
0

看看下面的例子。这将对您有所帮助。

    SELECT  DISTINCT RelatedTo,CAST(NotificationContent as nvarchar(max)) as NotificationContent,
                ID,
                Url,
                NotificationPrefix,
                NotificationDate
                FROM NotificationMaster as nfm
                inner join NotificationSettingsSubscriptionLog as nfl on nfm.NotificationDate between nfl.LastSubscribedDate and isnull(nfl.LastUnSubscribedDate,GETDATE())
  where ID not in(SELECT NotificationID from removednotificationsmaster where Userid=@userid) and  nfl.UserId = @userid and nfl.RelatedSettingColumn = RelatedTo
于 2011-12-19T05:52:27.797 回答
0
CREATE FUNCTION overlap_date(s DATE, e DATE, a DATE, b DATE)
RETURNS BOOLEAN DETERMINISTIC
RETURN s BETWEEN a AND b or e BETWEEN a and b or  a BETWEEN s and e;
于 2013-01-14T03:16:44.727 回答
0

在 MS SQL 上试试这个


WITH date_range (calc_date) AS (
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, [ending date]) - DATEDIFF(DAY, [start date], [ending date]), 0)
UNION ALL SELECT DATEADD(DAY, 1, calc_date)
FROM date_range 
WHERE DATEADD(DAY, 1, calc_date) <= [ending date])
SELECT  P.[fieldstartdate], P.[fieldenddate]
FROM date_range R JOIN [yourBaseTable] P on Convert(date, R.calc_date) BETWEEN convert(date, P.[fieldstartdate]) and convert(date, P.[fieldenddate]) 
GROUP BY  P.[fieldstartdate],  P.[fieldenddate];
于 2015-01-07T08:21:16.050 回答
0

另一种使用 BETWEEN sql 语句的方法

期间包括:

SELECT *
FROM periods
WHERE @check_period_start BETWEEN range_start AND range_end
  AND @check_period_end BETWEEN range_start AND range_end

排除期间:

SELECT *
FROM periods
WHERE (@check_period_start NOT BETWEEN range_start AND range_end
  OR @check_period_end NOT BETWEEN range_start AND range_end)
于 2015-10-15T12:35:22.517 回答
-2
SELECT * 
FROM tabla a 
WHERE ( @Fini <= a.dFechaFin AND @Ffin >= a.dFechaIni )
  AND ( (@Fini >= a.dFechaIni AND @Ffin <= a.dFechaFin) OR (@Fini >= a.dFechaIni AND @Ffin >= a.dFechaFin) OR (a.dFechaIni>=@Fini AND a.dFechaFin <=@Ffin) OR
(a.dFechaIni>=@Fini AND a.dFechaFin >=@Ffin) )
于 2017-06-22T16:05:57.890 回答