Read more
Check time/date overlap in MySQL
2012-12-19
MySQL
MySQL
There are four case make the overlap exists
(ns, ne, es, ee) = (new_start, new_end, existing_start, existing_end):
- ns - ne - es - ee: doesn't overlap and doesn't match (because ne < es)
- ns - es - ne - ee: ovarlaps and matches
- es - ns - ee - ne: ovarlaps and matches
- es - ee - ns - ne: doesn't overlap and doesn't match (because ns > ee)
- es - ns - ne - ee: ovarlaps and matches
- ns - es - ee - ne: ovarlaps and matches
SQL Statment Below method is found from internet while the second method is my own way usally use.
SELECT * FROM tbl WHERE
existing_start BETWEEN '$newStart' AND '$newEnd' OR
existing_end BETWEEN '$newStart' AND '$newEnd' OR
'$newStart' BETWEEN existing_star AND existing_end
SELECT * FROM tbl WHERE
( existing_start<='$newStart' AND existing_end>'$newStart' )
AND ( existing_start<'$newEnd' AND existing_start>'$newStart' )