I am having a trouble in querying my database.. I have a table :
**Room**
+----------------------------------+
|RoomNo---RoomStatusID---RoomTypeID|
| 1 --- 1 --- 1 |
| 2 --- 1 --- 1 |
+----------------------------------+
**ClientRoom**
+--------------------------------------------------------------------+
|ClientRoomNo---RoomNo--------ArrivalDate-------------DepartureDate |
| 1 --- 1 ---- 2011-10-03 1:00:00 ---- 2011-10-03 4:00:00|
| 2 --- 1 ---- 2011-10-03 5:00:00 ---- 2011-10-03 8:00:00|
+--------------------------------------------------------------------+
If I use this query =
SELECT Room.RoomNo, RoomType.RoomType
FROM Room INNER JOIN
RoomType ON Room.RoomTypeNo = RoomType.RoomTypeNo
FULL OUTER JOIN ClientRoom ON Room.RoomNo = ClientRoom.RoomNo
WHERE
(((ClientRoom.ArrivalDate <= '10-03-2011 1:00:00' AND
ClientRoom.ArrivalDate <= '10-03-2011 5:00:00') AND
(ClientRoom.DepartureDate <= '10-03-2011 1:00:00' AND
ClientRoom.DepartureDate <= '10-03-2011 5:00:00'))
OR Room.RoomStatusId = 1)
It would return
**ClientRoom**
+--------------------------------------------------------------------+
|ClientRoomNo---RoomNo--------ArrivalDate-------------DepartureDate |
| 2 --- 1 ---- 2011-10-03 5:00:00 ---- 2011-10-03 8:00:00|
+--------------------------------------------------------------------+
Because it reached up to 5:00am in the morning.
But when I change the Arrival and Departure to.. 5:00 – 8:00..
RoomNo 1 is still in the results.
I already tried NOT BETWEEN 🙁
You did not explain what result you want in what circumstances. But this condition
looks fishy, because the first part is redundant. You could simply say
Same thing for the next part:
This is the same as just saying:
Edit: From the comments I read it, that you have a given timespan (a,d) and want to check for each row, if your timespan overlaps with the timespan of the row (A,D). You want to see only non-overlapping rows.
This means your
WHEREclause must be like this quasicode:In full terms:
Edit 2:
What does the query with this fixes? “Show me all schedule rows, which do not conflict with my schedule.” But the query should be “Show me the rooms which do not have any conflicts with my schedule.” That is a completely different beast but easily translated into SQL: