Join table photos to query

Asked

Viewed 22 times

0

I have the following query that picks the user’s bookmarks ID=X

$sql="SELECT * FROM user WHERE id IN (SELECT user_2_id FROM favoritos WHERE
user_1_id =$id_user_logado)"; 

Now, how can I get the user photo of the users leaving this query? The id photo is stored in the user table in the field photo_p_id and the information related to the photo is in the table photos as the location of the.

Something like this:

$sql="SELECT * FROM user WHERE id IN 
(SELECT user_2_id FROM favoritos WHERE
user_1_id =$id_user_logado)

LEFT JOIN photos AS p

ON user.photo_p_id=p.id     

"; 

1 answer

0

I resolved it this way:

$sql = "SELECT  u.id, u.username, u.genero, u.idade, u.local, u.descricao,    u.status,u.last_login, u.photo_p_id, p.location, f.user_2_id, f.user_1_id

FROM user AS u 

LEFT JOIN photos AS p

ON u.photo_p_id=p.id 

LEFT JOIN favoritos AS f

ON f.user_2_id=u.id 

WHERE f.user_1_id =$id_user_logado

LIMIT 5;
";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.