Again booking system and dates ranges. I've read this topic Comparing date ranges and many others, but can't find answer :(
Please see SQLFiddle http://sqlfiddle.com/#!2/89b2b/2
I have booking table, that have ID, ObjectID, PeriodStart and PeriodEnd fields of type DATE in MySQL
My example data looks like this:
(1, 3, '2014-08-07', '2014-08-14'),
(2, 3, '2014-08-19', '2014-08-23'),
(3, 2, '2014-08-13', '2014-08-15'),
(4, 2, '2014-08-19', '2014-08-21');
My query dates interval is (updated, was $from = '2014-08-14';):
$from = '2014-08-15';
$to = '2014-08-19'
I'd like to get ObjectIDs that are free on query dates interval.
Problem is that, object's 2 first reservation ends on 2014-08-15 and we want to make new booking started from 2014-08-15. Same thing with ending date 2014-08-19. Existing reservations start on this day. It's ok for humans because we calculate date periods as nights, but how to tell same thing to MySQL?
For these variables
$from = '2014-08-15';
$to = '2014-08-19'
I'd like to get [2, 3] as the result of query.
For second query
$from = '2014-08-14';
$to = '2014-08-19'
I'd like to get only  [3] as the result of query, because ObjectID 2 is booked from 13 aug to 15 aug