# Select fields between 2 dates

**URL:** <https://forums.percona.com/t/select-fields-between-2-dates/688>\
**Category:** Other MySQL® Questions\
**Created:** [March 26, 2008, 11:24am UTC](https://forums.percona.com/t/select-fields-between-2-dates/688 "2008-03-26T11:24:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnadamson](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@johnadamson](https://forums.percona.com/u/johnadamson)\
**Post date:** [March 26, 2008, 11:24am UTC](https://forums.percona.com/t/select-fields-between-2-dates/688/1 "2008-03-26T11:24:51Z")

</div>

Hi,

I am trying to develop a booking system in PHP with MySQL but I am having trouble writing some code that checks to see if a new booking clashes with another. e.g. in the database I have a booking that runs from 2008-03-10 to 2008-03-20. When I add a booking I want to check to see if a booking already exists between these dates. Is this possible?

I am not an expert with MySQL but the closest I got is to carry out 2 query’s:

SELECT propertyid, arrival\_date, departure\_date FROM bookings WHERE arrival\_date BETWEEN ‘$arrival\_date’ AND ‘$departure\_date’

SELECT propertyid, arrival\_date, departure\_date FROM bookings WHERE departure\_date BETWEEN ‘$arrival\_date’ AND ‘$departure\_date’

but this obviously will not check if a booking is made in between these dates, e.g. if I try and add a booking from 2008-03-13 to 2008-03-18 it will not think that there is a collision of booking.

I hope I have made sence and not confused anyone.

Your help would be greatly appreciated.

Thanks.

John

---

<div class="post-metadata">

**Author:** ![beanblog](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/beanblog/32/774_2.png) [@beanblog](https://forums.percona.com/u/beanblog)\
**Post date:** [March 27, 2008, 8:33am UTC](https://forums.percona.com/t/select-fields-between-2-dates/688/2 "2008-03-27T08:33:08Z")

</div>

John

Since 2008-03-13 and 2008-03-18 are both between 2008-03-10 and 2008-03-20, I believe both of your statements will return a result.

Maybe I am misunderstanding the question?

Ben

---

<div class="post-metadata">

**Author:** ![johnadamson](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@johnadamson](https://forums.percona.com/u/johnadamson)\
**Post date:** [March 28, 2008, 4:46am UTC](https://forums.percona.com/t/select-fields-between-2-dates/688/3 "2008-03-28T04:46:34Z")

</div>

Hi Bed,

Unfortunately not. I have now condensed the 2 querys into one:

SELECT propertyid, arrival\_date, departure\_date FROM bookings WHERE arrival\_date = ‘$arrival\_date’ AND ‘$departure\_date’ ) OR ( departure\_date BETWEEN ‘$arrival\_date’ AND ‘$departure\_date’ )

which works a treat if the arrival or departure date crosses an existing booking, but it still returns an empty result if I try and add a booking 2008-03-13 to 2008-03-18.

Very strange!
