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 the one who sent the friendship invitation ...
I need to know which of the user's friends on the page I accessed ( $userPagina
) and their information ( pnome, snome, foto
), so far so good, I've done 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 I access meusite.com/Alex
it correctly returns my friends (Igor and Maria), however, I also want to know how many friends in common Alex's friends ( $userPagina
) have with the logged in user that accessed his profile ( $userLogado
), that is, how many friends in common Igor and Maria have $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 error, since I'm identifying u.
in INNER JOIN
... How to fix it? And, is my logic for this count correct?
Structure of tables:
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;