mysql - sql query to check if 2 events overlaps -


my table has 3 columns name, start_date , end_date need insert record in table if date of new event don't overlap existing event. so, have event start date 1/1/2015 , end date 31/12/2015 can't insert event if has dates these:

  • 31/12/2014 - 1/1/2016
  • 2/1/2015 - 30/12/2015
  • 31/12/2014 - 1/6/2015
  • 2/1/2015 - 1/1/2016

or, talking integrers:

  • existing record range 2...6

possible invalid records:

  • 1..3
  • 3..5
  • 5..7
  • 1..7

what's shorter expression match condition?

any 2 ranges overlap if (and if) first start smaller second end, while second start smaller first end.

therefor, want test incoming record has start date , end date not match condition record in table.

here visual proof - of ways 2 lines can overlap:

1. s1|--------|e1 s2|--------|e2   2. s1|-------|e1      s2|--------|e2   3.      s1|--------|e1 s2|--------|e2   4. s1|-------------------|e1      s2|--------|e2   5.      s1|--------|e1 s2|-------------------|e2 

as can see, s1 smaller e2, while s2 smaller e1.

this not case when 2 lines not overlap:

1. s1|--------|e1                  s2|--------|e2   2.                  s1|--------|e1 s2|--------|e2 

as can see here, either s1 bigger e2 or s2 bigger e1.

the actuall data type irrelevant long it's comparable.


Comments