QUERY - Can anyone help me mount this query?

Asked

Viewed 46 times

4

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

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

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

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

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

Thank you!

  • That’s easy to do, but what you’ve tried?

  • SELECT p.descr,p.dt_hr, (SELECT U1.name FROM users U1 WHERE U1.id = f.id_friend) AS name FROM Friends f JOIN users u ON u.id = f.id_user POSTS join p ON p.id_user = f.id_friend WHERE u.id = 1 ORDER BY p.dt_hr DESC

  • The one I rode was the one above, but I need to return two fields of this subselect, and it only returns 1. In case, I need the name and image...

  • and how hard it is to make another Join with it?

  • I’m not succeeding, could help?

  • see if this is what you wanted.

  • That’s right, it’s because I was already making a Join at users, I didn’t think I needed to make one more, you know? But thanks man. Thank you very much!!

Show 2 more comments

1 answer

3


Do;

 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

Browser other questions tagged

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