Hi everyone I am in the process of building a room allocating system that checks if a room is available on a given date. There are about 20 unique room ids and I've just included as snippet of several above.
So for example if the user types in the date of 2012-01-01 I would want the room_id of 1 to be excluded from the results because as seen above it has been already booked for that date.
In a sense any match found between the user's in-putted date should cause the entire list of corresponding room_id's to be excluded from the list.
I used the below SQL query:
SELECT DISTINCT room_id
FROM room_booking
WHERE date_booked<>'$date'
Unfortunately it doesn't work because although it excludes the result for a single row, it includes the rest of the results for a given room_id?
To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE statement.
[^] Wildcard to exclude characters - SQL Server (Transact-SQL) | Microsoft Learn.
Not tried this, but here goes
SELECT DISTINCT room_id
FROM room_booking
WHERE room_id NOT IN
(SELECT DISTINCT room_id
FROM room_booking
WHERE date_booked = '$date'
)
You could use a NOT EXISTS
SELECT DISTINCT room_id
FROM room_booking rb1
WHERE NOT EXISTS( SELECT 1
FROM room_booking rb2
WHERE rb2.date_booked = <<date>>
AND rb2.room_id = rb1.room_id )
or a NOT IN
SELECT DISTINCT room_id
FROM room_booking
WHERE room_id NOT IN (SELECT room_id
FROM room_booking rb2
WHERE rb2.date_booked = <<date>>)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With