Programing

MySQL에서 날짜 범위 중복 확인

lottogame 2020. 10. 26. 07:37
반응형

MySQL에서 날짜 범위 중복 확인


이 테이블은 세션 (이벤트)을 저장하는 데 사용됩니다.

CREATE TABLE session (
  id int(11) NOT NULL AUTO_INCREMENT
, start_date date
, end_date date
);

INSERT INTO session
  (start_date, end_date)
VALUES
  ("2010-01-01", "2010-01-10")
, ("2010-01-20", "2010-01-30")
, ("2010-02-01", "2010-02-15")
;

범위 간의 충돌을 원하지 않습니다.
하자 우리가에서 새 세션을 삽입해야 말 2010-01-052010-01-25 .
충돌하는 세션을 알고 싶습니다.

내 질문은 다음과 같습니다.

SELECT *
FROM session
WHERE "2010-01-05" BETWEEN start_date AND end_date
   OR "2010-01-25" BETWEEN start_date AND end_date
   OR "2010-01-05" >= start_date AND "2010-01-25" <= end_date
;

결과는 다음과 같습니다.

+----+------------+------------+
| id | start_date | end_date   |
+----+------------+------------+
|  1 | 2010-01-01 | 2010-01-10 |
|  2 | 2010-01-20 | 2010-01-30 |
+----+------------+------------+

그것을 얻는 더 좋은 방법이 있습니까?


깡깡이


한 번 작성한 캘린더 응용 프로그램으로 이러한 쿼리가있었습니다. 다음과 같은 것을 사용한 것 같습니다.

... WHERE new_start < existing_end
      AND new_end   > existing_start;

업데이트 이것은 확실히 작동합니다 ((ns, ne, es, ee) = (new_start, new_end, existing_start, existing_end)) :

  1. ns-ne-es-ee : 겹치지 않고 일치하지 않음 (ne <es 때문에)
  2. ns-es-ne-ee : 중복 및 일치
  3. es-ns-ee-ne : 중복 및 일치
  4. es-ee-ns-ne : 겹치지 않고 일치하지 않음 (ns> ee 때문에)
  5. es-ns-ne-ee : 중복 및 일치
  6. ns-es-ee-ne : 중복 및 일치

여기 바이올린이 있습니다


SELECT * FROM tbl WHERE
existing_start BETWEEN $newStart AND $newEnd OR 
existing_end BETWEEN $newStart AND $newEnd OR
$newStart BETWEEN existing_start AND existing_end

if (!empty($result))
throw new Exception('We have overlapping')

이 3 줄의 SQL 절은 필요한 겹침의 4 가지 경우를 다룹니다.


Lamy의 대답은 좋지만 조금 더 최적화 할 수 있습니다.

SELECT * FROM tbl WHERE
existing_start BETWEEN $newSTart AND $newEnd OR
$newStart BETWEEN existing_start AND existing_end

이렇게하면 범위가 겹치는 네 가지 시나리오를 모두 포착하고 그렇지 않은 두 시나리오는 제외합니다.


나는 비슷한 문제에 직면했다. 내 문제는 차단 된 날짜 범위 사이의 예약을 중지하는 것이 었습니다. 예를 들어 5 월 2 일부터 5 월 7 일까지 숙소 예약이 차단됩니다. 예약을 감지하고 중지하려면 겹치는 날짜를 찾아야했습니다. 내 솔루션은 LordJavac과 유사합니다.

SELECT * FROM ib_master_blocked_dates WHERE venue_id=$venue_id AND 
(
    (mbd_from_date BETWEEN '$from_date' AND '$to_date') 
    OR
    (mbd_to_date BETWEEN  '$from_date' AND '$to_date')
    OR
    ('$from_date' BETWEEN mbd_from_date AND mbd_to_date)
    OR      
    ('$to_date' BETWEEN mbd_from_date AND mbd_to_date)      
)
*mbd=master_blocked_dates

작동하지 않으면 알려주세요.


Given two intervals like (s1, e1) and (s2, e2) with s1<e1 and s2<e2
You can calculate overlapping like this:

SELECT 
     s1, e1, s2, e2,
     ABS(e1-s1) as len1,
     ABS(e2-s2) as len2,
     GREATEST(LEAST(e1, e2) - GREATEST(s1, s2), 0)>0 as overlaps,
     GREATEST(LEAST(e1, e2) - GREATEST(s1, s2), 0) as overlap_length
FROM test_intervals 

Will also work if one interval is within the other one.


Recently I was struggling with the same issue and came to end with this one easy step (This may not be a good approach or memory consuming)-

SELECT * FROM duty_register WHERE employee = '2' AND (
(
duty_start_date BETWEEN {$start_date} AND {$end_date}
OR
duty_end_date BETWEEN {$start_date} AND {$end_date}
)
OR
(
{$start_date} BETWEEN duty_start_date AND duty_end_date
OR
{$end_date} BETWEEN duty_start_date AND duty_end_date)
);

This helped me find the entries with overlapping date ranges.

Hope this helps someone.

참고URL : https://stackoverflow.com/questions/2545947/check-overlap-of-date-ranges-in-mysql

반응형