QUERY - Can anyone help me build this query?

4

I have these 3 tables below: "users", "friends" and "posts".

I want to get all my friends who are in the friends table and display their posts, taking their name and their image (which are in the users table).

Imagine a news feed, where I have to show posts.

I wanted to try doing just with JOINS, without using subselect (which was the way I found it to do)

Thank you!

    
asked by anonymous 03.01.2017 / 13:11

1 answer

3

Make;

 SELECT p.descr,p.dt_hr , u1.name, u1.img
    FROM friends f 
    JOIN users u 
    ON u.id = f.id_user 
    JOIN posts p 
    ON p.id_user = f.id_friend 
    join users u1 
    on u1.id = f.id_friend
    WHERE u.id = 1 
    ORDER BY p.dt_hr DESC
    
03.01.2017 / 13:18