6
I have the following tables:
users:
id | usuario | pnome | snome | foto
1 | Igor | Igor | Souza | perfil.png
2 | Alex | Alex | Khal | foto.jpg
3 | Maria | Maria | Silva | foto.png
friendships:
id | amigo1 | amigo2 | estatus (2 significa que são amigos)
1 | Igor | Alex | 2
2 | Igor | Maria | 2
3 | Maria | Alex | 2
In the table above, amigo1
is who sent the invitation of friendship...
I need to know which user friends of the accessed page ($userPagina
) and their information (pnome, snome, foto
), so far so good, I did the following query:
SELECT u.usuario, u.pnome, u.snome, u.foto
FROM amizades a
INNER JOIN usuarios u
ON (u.usuario = a.amigo1 AND a.amigo1 <> '$userPagina') OR (u.usuario = a.amigo2 AND a.amigo2 <> '$userPagina')
WHERE (a.amigo1 = '$userPagina' OR a.amigo2 = '$userPagina') AND a.estatus = 2
So, when accessing meusite.com/Alex
he returns his friends (Igor and Maria) correctly, but I also want to know how many friends in common Alex’s friends ($userPagina
) has with the logged-in user who accessed your profile ($userLogado
), that is, how many mutual friends Igor and Maria have with the $userLogado
. So I tried adding the following data in the query:
SELECT u.usuario, u.pnome, u.snome, u.foto, COUNT(c.id) as comum
FROM amizades a
INNER JOIN usuarios u
ON (u.usuario = a.amigo1 AND a.amigo1 <> '$userPagina') OR (u.usuario = a.amigo2 AND a.amigo2 <> '$userPagina')
LEFT JOIN (SELECT id FROM amizades WHERE (amigo1 = u.usuario AND amigo1 <> '$userLogado' AND estatus = 2) OR (amigo2 = u.usuario AND amigo2 <> '$userLogado' AND estatus = 2)) c ON (u.usuario = A.amigo1 OR u.usuario = A.amigo2)
WHERE (a.amigo1 = '$userPagina' OR a.amigo2 = '$userPagina') AND a.estatus = 2
However, when accessing meusite.com/Alex
again, only the error is returned:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u.usuario' in 'where clause'
But I did not understand the reason for this mistake, since I am identifying the u.
in the INNER JOIN
... How to tidy up? And, my logic for this count is correct?
Table structure:
users:
CREATE TABLE IF NOT EXISTS `usuarios` (
`id` int(11) NOT NULL,
`usuario` varchar(90) NOT NULL,
`pnome` varchar(30) NOT NULL,
`snome` varchar(60) NOT NULL,
`foto` varchar(90) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
INSERT INTO `usuarios` (`id`, `usuario`, `pnome`, `snome`, `foto`) VALUES
(5, 'Igor', 'Igor', 'Souza', 'perfil.png'),
(4, 'Alex', 'Alex', 'Khal', 'foto.jpg'),
(3, 'Maria', 'Maria', 'Silva', 'foto.png');
ALTER TABLE `usuarios`
ADD PRIMARY KEY (`id`);
ALTER TABLE `usuarios`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=13;
friendships:
CREATE TABLE IF NOT EXISTS `amizades` (
`id` int(11) NOT NULL,
`amigo1` varchar(90) NOT NULL,
`amigo2` varchar(90) NOT NULL,
`estatus` int(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
INSERT INTO `amizades` (`id`, `amigo1`, `amigo2`, `estatus`) VALUES
(10, 'Igor', 'Alex', 2),
(9, 'Igor', 'Maria', 2),
(8, 'Maria', 'Alex', 2);
ALTER TABLE `amizades`
ADD PRIMARY KEY (`id`);
ALTER TABLE `amizades`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=13;
I put Alex and Igor because people who read the question will be easier to test. But in your case just replace Alex with $userLogado and Igor with $userPagina.
– Sérgio Mucciaccia
I added an example image to clarify my question
– Igor
Oh yes, you want the friends in common that userLogado has with every user friend of the page, that’s it?
– Sérgio Mucciaccia
Exactly! That’s right! Show the information of each user friend of the page (name, snome, photo) and how many friends in common with the
$userLogado
, in which case it would be 1– Igor
Okay, now the query is the way you asked! Just replace José with $userLogado and Alex with $userPagina! The other day I try to explain the consultation...
– Sérgio Mucciaccia
It is working, however, when accessing
meusite.com/Alex
it lists the userMaria
(correctly), butamigos_em_comum
returns 3 instead of 1 (1 because between me (Igor
) andMaria
there is only one friend, who is himselfAlex
).– Igor
Let’s go continue this discussion in chat.
– Sérgio Mucciaccia