Here are my tables:
1): table friends:
id_invitation
id_exp
id_des
date_invitation
active
2): table users:
id
prenom
nom
email
password
I have two queries:
1: The first returns me list of friends.
SELECT *
FROM users U
JOIN friends F
ON ( U.id = F.id_exp AND F.id_des = :id )
OR ( U.id = F.id_des AND F.id_exp = :id )
WHERE U.id <> :id
AND F.active = 1
2: the second returns only the mutual friends between two users.
SELECT u.id
, u.nom
, u.prenom
FROM users u
JOIN
( SELECT id_exp
, id_des
FROM friends
WHERE id_exp IN(:id_exp, :id_des)
AND active = 1
UNION
SELECT id_des
, id_exp
FROM friends
WHERE id_des IN(:id_exp, :id_des)
AND active = 1
) tmp
ON tmp.id_des = u.id
GROUP
BY u.id
HAVING COUNT(*) = 2
Small correction:
1 is a common friend between 2 and 3 since he is a friend with both 2 and 3 (if you want to collect the common friend at 2 and 3, you will have 1). 4 is friend with 2 but not with 3. And 3 also is friend with 5 and 6, I want to search in friends list of 2, recover friends from 2 but who are not friends with 3 (in this case 4). I want to return 4 only and not 4,5,6
More informations: I want that when 3 visits the list of uncommon friends of 2, it sees 4. I want to return only friends of 2 who are not as friendly with 4.
I have a little trouble explaining it.
And I have tried that code:
SELECT
*
FROM
users U
INNER JOIN friends F
ON ( U.id = F.id_exp AND F.id_des = :id )
OR ( U.id = F.id_des AND F.id_exp = :id )
WHERE
(U.id <> :id_k
AND F.active = 1)
AND NOT IN
(SELECT u.id,
u.nom, u.prenom, u.avatar
FROM users u
INNER JOIN
(
SELECT id_exp, id_des
FROM friends
WHERE id_exp IN(:id_exp, :id_des)
AND active = 1
UNION
SELECT id_des, id_exp
FROM friends
WHERE id_des IN(:id_exp, :id_des)
AND active = 1
) tmp ON tmp.id_des = u.id
GROUP BY u.id
HAVING COUNT(*) = 2)
But it does not work at worst, it produces errors and the section of the page in question does not appear. Thank you.
I believe you already have the ingredients you need for both mutual and non-mutual friend states using the current query, it's just a matter of how you now use it.