Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:57:10+00:00 2026-06-16T17:57:10+00:00

I am trying to develop a function which shows me friends of friends, and

  • 0

I am trying to develop a function which shows me friends of friends, and how many mutual friends I have with those users. So far, I have been able to do the functions separately but not together.

Here is the main view I am using called userfriends. Where user1 is the first user and user2 is the friend.

enter image description here

This is the function I have developed to see mutual friends between two users

SELECT id FROM users
WHERE id IN (
    SELECT user2 FROM userfriends WHERE user1 = me
) AND id IN (
    SELECT user2 FROM userfriends WHERE user1 = second
)

Users is a main table which can link the user id found in the userfriends table to information about the user. Me and second are variables in the stored procedure which emulate the search for first and second users. This function works to plan.

The second function I have is to see all the users who are friends with my friends, but not with me.

SELECT user2 AS id
FROM userfriends
    WHERE user1 IN (
        #Selects my friends
        SELECT user2 FROM userfriends
        WHERE user1 = me
    ) 
    AND user2 <> me #Makes sure is not me
    AND user2 NOT IN ( #Makes sure not already friend
        SELECT user2 FROM userfriends
        WHERE user1 = me
    )

Again, all working to plan and me represents the user id. This will return a list of all my friends friends.

What I want to be able to get instead of a list of mutual users, or a list of my friends friends is:

A table which has my friends friend user ID and how many mutual friends me and that user shares. Etc: user: 1, friends_in_common: 103. If I’m not clear enough please ask and I will try and make it clearer. The two functions do it by themselves, but I’m just not sure how to merge it together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:57:11+00:00Added an answer on June 16, 2026 at 5:57 pm
    -- use a self-join of userfriend*userfriend
    -- to find all the friends that moi&toi have in common
    -- , and count them.
    -- (optionally) suppress the rows for (toi<=moi)
    -- , because there are basically the same as the corresponding rows with
    -- (toi>moi)
    -- -----------------------------------
    SELECT DISTINCT 
       uf1.user1 AS moi
       , uf2.user1 AS toi
       , COUNT(*) AS cnt
       FROM userfriend uf1
       JOIN userfriend uf2 ON uf1.user2 = uf2.user2
       WHERE uf1.user1 < uf2.user1 -- Tie breaker, symmetry remover
       GROUP BY uf1.user1, uf2.user1
       ; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a function which will allow me to input new elements
I have been asked to re-develop an old php web app which currently uses
I am trying to develop an optimization function that will determine which elements in
I am trying to develop a script in Python which would function like the
I'm trying to develop a function that can sort through a string that looks
Im trying to develop my first ASP.NET MVC web app and have run into
I am trying to develop an application for my localhost on which I can
I was trying to develop a video management software for which I was evaluating
I am trying to develop the XMPP Gateway which can send/receive from standard XMPP
Im trying to develop a small site which retrieves a results from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.