the title might be a little bit confusing, let me explain, 😉
I have 3 tables:
[names]
n_id;name
1;Jeff
2;Adam
[books]
b_id;title
1;Book1
2;Book2
[read]
n_id;b_id
The table [read] is a table with read books.
if Adam reads “Book1” the item in [read] looks like this:
2;1
so far, so good.
Now, is there a way to know which books werent read by a person?
We know that only Adam read a book “Book1”, so the query should output something like this:
n_id;name;b_id;title
1;Jeff;1;Book1
1;Jeff;2;Book2
2;Adam;2;Book2
is it possible to do this in 1 query or do I need some scripting?
You can use a
CROSS JOINto get all possible combinations ofnamesandbooksand then use aLEFT JOINonreadwithIS NULLto remove rows that exist there.The
LEFT JOINreturnsNULLfor all joined columns where no rows exist, so checking ifr.n_id IS NULLremoves those rows where the join actually found rows inread.