Join table photos to query

0

I have the following query that takes 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 picture of the users who leave this query? The id of the photo is saved in the user's table in the photo_p_id field and the photo information is in the photos table as the location of the photo.

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     

"; 
    
asked by anonymous 30.01.2016 / 15:33

1 answer

0

I have decided 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;
";
    
30.01.2016 / 15:45