Answer the question
In order to leave comments, you need to log in
How to properly build a JOIN SQL query?
Hello.
I stumbled again, and here's the problem, I don't know how to build the query correctly:
there is a black list
of 2 tables:
1. users : id | name | surname | photo
2. black_list : id | ban_id | user_id | date
You need to get all records from black_list where user_id = 3 for example
and from users only name and photo.
And for users, we are looking for id, we take id from 1 request to black_list , the ban_id
field Is JOIN
needed here too, or do we need to make 2 requests?
given that the first request to black_list will return several records, which means that you will need to get several rows from the users table, doing everything in a loop is also not an option..
(easier - you need to get the name, photo having several id and get the data for each)
Please tell me how to be?
Answer the question
In order to leave comments, you need to log in
If understood correctly
SELECT u.name, u.photo
FROM black_list AS b
JOIN users AS u ON b.ban_id=u.id
WHERE b.user_id IN (3)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question