I am working on a web app where it’s possible to make a reservation of a meeting room. Let me first explain you how my database looks like.
I have table Reservations with the following design
ID --> int
ROOMID --> int
DATE_BEGIN --> DATETIME
DATE_END --> DATETIME
I have also a table ROOM with the following design
ID --> int
NAME --> VARCHAR(30)
Now an insert of a row in the table reservation looks like this
ID --> 1
ROOMID --> 2
DATE_BEGIN --> 2012-01-01 12:02:33
DATE_END --> 2012-01-01 14:00:00
Now what I do is, I enter a begin date and an end date. And when I push a button e.g. Check availability it gives back all the room names which are available for this given date range.
For now I have this query:
SELECT zaa.NAME
FROM ARTICLES_ZAAL zaa
INNER JOIN ARTICLES_RESERVERING res
ON zaa.ID =res.ZAALID
WHERE res.DATUM_BEGIN <> @DATUM_BEGIN
AND res_DATUM_EINDE <> @DATUM_EINDE
I know that there is still a lot missing but the problem is what. I am doing all of this in a function which is called by a web service.
Could anybody help please ?
kind regards.
The other answers do not handle requests for time that overlap reservations, and BETWEEN is not a good choice because it is inclusive (so if the reservation ended at 2 PM, and you want one that starts at 2 PM, it would be false, which is not correct).
Try this SQL fiddle. You can mess around with the requested start and end dates to check my work, which is working properly as far as I’m concerned.
http://sqlfiddle.com/#!3/cb682/25
Excerpt: