Bear with me please because my SQL is not that good.
I want to write a query that takes into consideration information from 3 different tables.
Let me explain the situation: When a customer books a room, they have the preference of several rooms and the rooms can be booked on individual days (one unique booking has to be made per day) at different times.
I now want to write a query that will check if a room is available on a certain time of the day. These are the tables I have on my database:
room
This table stores information about each room
Fields:
> id (primary key)
> capacity
> img
> notes
> building_id
> roomstructure_id
> lecturestyle_id
request
This table stores message about each request made
Fields
> id (primary key)
> day_id
> period_id
> preference_id (foreign key from preference.id)
preference
This table stores information about all the room preferences.
> id (PK)
> request_id (foreign key from request.id)
> room_id (foreign key from room.id)
I hope what I am trying to do has become apparent.
If for example I am checking whether room C233 is booked on Monday, 09:00 - I would need to look up the room information in the room table and then use the room_id in the preference table to look up the request_id.
Once I have the request_id which has been linked to a specific room (in the room preference table) I can see whether day_id=1 and period_id=1 in the request table. If day_id=1 and period_id=1 then it means that the room is booked at Monday, 09:00.
This means that room is not available so it isn't counted but if this row could not be found then it would be counted.
This is the SQL I've written so far - its very simple but it doesn't do everything I want it to do:
SELECT COUNT(*) totalCount FROM ts_room
In addition to this, I have been offered advice on another forum - this code here. But it doesn't work at all. I don't think it meets my criteria:
SELECT COUNT(*) totalCount
FROM room a
INNER JOIN preference b
ON a.id = b.room_id
INNER JOIN ts_request c
ON b.request_id = c.preference_id
WHERE c.day_id = 1 AND c.period_id = 1
Nothing returns when I run this query :-(