MySQL find any records between date range -


i'm building simple reservation form. let's say, want book car 2015-07-12 2015-07-21.

in database have record, there allready reserved car (car_id 18) 2015-07-15 2015-07-20.

here query, check if there reserved car:

select * `reservation` `reservation_from` <= '2015-07-12' , `reservation_to` <= '2015-07-21' , `car_id` = 18 

after query, got record, there reserved car. good.

and now, let's say, want reserve car 2015-07-09 2015-07-19.

this time, got empty result. there record, car reserved 2015-07-15 2015-07-20!

i have played <= >= operators, , no luck.

yes, both columns in date format.

what i'm missing? how record, there allready booked car within selected date range?

and yes, mysql not strongest side. thanks!

you want any overlap, not complete overlap. logic is:

select * `reservation` `reservation_from` <= '2015-07-21' ,       `reservation_to` >= '2015-07-12' ,        `car_id` = 18; 

that is, there no car available if reservation starts before period ends , ends after period starts.


Comments