N
N
noob-sql2020-10-27 07:07:14
SQL
noob-sql, 2020-10-27 07:07:14

How to filter data from one table using data from another table?

photos:
[id, user_id, (...url)]
|1, 1| - photo #1, owned by user #1
|2, 4| - photo #2 belonging to user #4
|3, 2| - photo #3 belonging to user #2
|4, 3| - photo #4 belonging to user #3
|5, 5| - photo #5, owned by #5 user

viewed:
[id, user_id, photo_id]
|1, 2, 4| - #2 user viewed photo #4 belonging to #3 user
|2, 3, 2| - #3 user viewed photo #2 belonging to #4 user
|2, 3, 5| - #3 user viewed photo #5 belonging to #5 user

How can I get only those photos that user #3 has NOT viewed among these data? And also try to filter the photos of the user #3 himself, that is, photo #4 (mysql)
That is:
result:
[id, user_id, (...url)]
|1, 1| - photo #1, owned by user #1
|3, 2| - photo #3, owned by user #2

As I understand it, you need to somehow poke joins, but I still can't connect it properly in my head. I would like to see the decision of knowledgeable people. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-10-27
@noob-sql

WHERE ... IN (SELECT ...)
WHERE ... NOT IN (SELECT ...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question