I’m storing some intervals in the SQL, example:
id INT
from DATE
to DATE
How can I check, using only one condition (if possible) if a NEW interval conflict with a existing one?
Examples:
|-----------| (from 1 to 5)
|-----------| (from 2 to 6)
|--| (from 3 to 4)
|--| (from 7 to 8)
Every interval (in the first three) has some conflict with the other two intervals… Except the last one that’s alone.
—
This check can be achieved using some condition like:
WHERE (`from` <= $FROM and `to` >= $TO)
But this only check intervals that contains the new one… Not the other intervals that has some intersections OR the ones that’s inside the new one.
Maybe something like this?
WHERE NOT (`from` < $FROM and `to` < $TO) AND NOT (`from` > $FROM and `to` > $TO)
Obs.: I need to find the collisions to alert the user that this new period already exist or get in conflict with an existin one.
Note this works for the case where the new range overlaps the whole range, when it only partially overlaps and when it encompasses it as well.