I have four tables:
days
CREATE TABLE days (
day text priamry key
);
These days run monday to saturday.
times
CREATE TABLE times (
time time Primary Key,
peak text
);
The time run from 8:00 am to 9:00 pm in hour slots E.g. 8:00, 9:00, 10:00, 11:00
activities
CREATE TABLE activities (
activity text primary key
);
and planner
CREATE TABLE planner (
day text foriegn key references days (day)
time time foriegn key references times (time)
activity text foriegn key references activities (activity)
member bigint
primary key (day, time, member)
);
The planner table will have data like:
friday, 09:00, squash_court1 , 2
friday, 09:00, squash_court2 , 3
friday, 09:00, squash_court3 , 1
What I am wanting to do i make a list of all the none booked times for these three courts
So I would have a list like
time activity
08:00 squash_court1
10:00 squash_court1
...rest of times...
08:00 squash_court2
10:00 squash_court2
...rest of times...
08:00 squash_court3
10:00 squash_court3
...rest of times...
an the reason these is no 9:00 between 8:00 and 10:00 is because it has been booked
EDIT
At the moment I have the basic join of:
SELECT time , activity FROM times, activities;
All I need now is the WHERE clause to remove the ones that are booked in the planner table.
Thank you for any advice on this matter.
This can be done in various ways.
LEFT JOIN / WHERE .. IS NULLoften produces the fastest plan in PostgreSQL:I use parenthesis to make clear you want to
CROSS JOIN(same as a comma between the tables)daysandtimesandactivitiesfirst. These parenthesis are redundant, because tables are joined from left to right by default.The
JOINcondition is just a shorter form of:Another way would be a
NOT EXISTSsemi-join: