These are the corresponding relationships (approved) I have set up in the database (friendship table) :
Code:
Alfred Karen
Duncan John
James Alfred
James Frank
James Neil
James Philip
Karen Frank
Neil Duncan
Neil Frank
Neil Karen
Philip John
Philip Karen
Ross Inziman
The query below almost gives the right answer I think. Once you have changed the approved status on some of your friend relationships you need to check through the data to make sure it's giving the correct result. For the below query the user_id = 6 relates to James.
Code:
SELECT
u1.username as person,
u2.username as friend,
u3.username as friend_of_friend
FROM users u1
JOIN friendships f1 ON u1.id=f1.user_id
JOIN users u2 ON f1.friend_id=u2.id
JOIN friendships f2 ON u2.id=f2.user_id
JOIN users u3 ON f2.friend_id=u3.id
WHERE f1.approved=1 AND u1.id=6 AND f2.approved=1;
The results given are as follows:
Code:
person friend friend_of_friend
James Alfred Karen
James Philip Karen
James Neil Karen
James Neil Frank
James Philip John
James Neil Duncan
I'm sure there is a better answer to this, however i've only spent 15 minutes on it
Edit : As you can see there is a problem there with duplication of data,
i.e.
if James knows Alfred and Alfred knows Karen then James knows Karen,
however, this information is duplicated because if James knows Neil and Neil knows Karen then James knows Karen.
See what I mean?