AaronSantos
AaronSantos

Reputation: 159

Query to get all reservations that either begin or end within a time range?

I believe this is a quite trivial query(I did search SO and Google but came to nothing), SQL isn't definitely my strong point, and I've been on a coffee rampage for the past hours...

I'm working on a simple booking system.

I have a reservations table like the one bellow:

id | room_id | checkin_date  | checkout_date |
---+---------+---------------+---------------+
1  |    12   | 2012-10-12    |  2013-01-14   |
2  |    9    | 2012-12-08    |  2013-01-10   | 
3  |    4    | 2012-12-19    |  2012-12-26   | 
4  |    6    | 2012-11-12    |  2012-12-30   |  
5  |    3    | 2012-12-11    |  2012-01-11   |

How can I get all the reservations that either end or begin inside a certain interval(the data will then be used to populate an availability calendar).

Sorry about not posting my own Sql, but all of my many attempts have been futile(and shamefully bad).

Thanks in advance.

Upvotes: 2

Views: 89

Answers (1)

Bohemian
Bohemian

Reputation: 424993

select * from reservations 
where checkin_date between ? and ?
or checkout_date between ? and ?

You need to supply the start and end dates in the placeholders, for example

select * from reservations 
where checkin_date between '2012-01-01' and '2012-01-66'
or checkout_date between '2012-01-01' and '2012-01-66'

Upvotes: 1

Related Questions