Query to return followers of friends

Asked

Viewed 48 times

1

I’m starting in SQL and I’m having trouble making a query with multiple lines returned. I’m creating a generic twitter clone.

Goal: Return users who are followed by the people who logged user follows and display as people he may know, excluding himself and the people he already follows.

Tables

CREATE TABLE `usuarios` (
   `id` int(11) NOT NULL,
   `usuario` varchar(60) NOT NULL,
   `email` varchar(100) NOT NULL,
   `foto` varchar(80) DEFAULT 'imagens/semfoto.png'
);

CREATE TABLE `usuarios_seguidores` (
   `id_usuario_seguidor` int(11) NOT NULL,
   `id_usuario` int(11) NOT NULL,
   `seguindo_id_usuario` int(11) NOT NULL,
   `data_registro` datetime DEFAULT CURRENT_TIMESTAMP
);

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

This command below returns the people the logged in user($id_usuario) follows, however this would be only the first part of the return.

SELECT seguindo_id_usuario
  FROM usuarios AS u
  LEFT JOIN usuarios_seguidores AS us
    ON (us.id_usuario = u.id)
 WHERE id_usuario = $id_usuario;

Thanks for the help.

No answers

Browser other questions tagged

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