If A is a friend of B, then B is also a friend of A? Wouldn't it be better to use only a link instead of two links (and instead of two lines in friends_links)? Then you need to use two status columns, status1 and status2, and A is a friend of B only if status1 = status2 = "a".
There are many ways to show mutual friends, for example:
SELECT friend_id FROM friend_links WHERE friend_links.user_id = $user1 or friend_links.user_id = $user2 AND NOT (friend_links.friend_id = $user1 or friend_links.friend_id = $user2) GROUP BY friend_id HAVING Count(*)>1
And this request is displayed for each user and anyone who is not his friend:
SELECT users.user_id, users.first_name, users_1.user_id, users_1.first_name FROM users INNER JOIN users users_1 ON users.user_id <> users_1.user_id WHERE NOT EXISTS (SELECT * FROM friend_links WHERE friend_links.user_id = users.user_id AND friend_links.friend_id = users_1.user_id)
(The only thing I did not check is the status of friendship, but it is easy to add this check).
I am still working on this, but it is not easy to combine these two togheter requests. So this is not quite the answer, I am just showing some ideas that I have tried.
But what do you need exactly? A query that returns each user with someone who is not his friend and number of friends, or user_id is already specified?
With some code, this is not a problem to answer your question ... but there should be a good way just using SQL! :)
EDIT:
I'm still wondering if there is a better solution for this, in particular, the following query may be very slow, but it looks like this might work:
SELECT users_1.user_id, users_2.user_id, Sum(IF(users_1.user_id = friend_links.user_id AND users_2.user_id = friend_links_1.friend_id, 1, 0)) As CommonFriend FROM users users_1 INNER JOIN users users_2 ON users_1.user_id <> users_2.user_id, (friend_links INNER JOIN friend_links friend_links_1 ON friend_links.friend_id = friend_links_1.user_id) GROUP BY users_1.user_id, users_2.user_id HAVING Sum(IF(users_1.user_id = friend_links.user_id AND users_2.user_id = friend_links.friend_id, 1, 0))=0
(as before, I did not check the status of friendship)
If the user is specified, you can put WHERE users_1.user_id=$user1 , but itβs better to just leave one user table and filter the next INNER JOIN with that user.