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 8721561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:14:31+00:00 2026-06-13T07:14:31+00:00

I am creating a small social network in which when a user posts anything.I

  • 0

I am creating a small social network in which when a user posts anything.I will get ids of his friends to whom he referred the post.Now the problem is that the users who will be referred can be between 1 or 10. So now suppose that a user posted a status and referred 6 friends.

Now i need to find mutual friends between the user who posted the status and the people referred one by one.Meaning that suppose user who posted the status has id 1 and the people who are referred have 2,3,4,5,6,7.

So what the query will do it will find mutual friends between 1 and 2 then 1 and 3 then 1 and 4 and so on till 7.So at the end i will be getting a result containing the mutual friends between the user who posted and the people who are referred(or tagged we can say).

I’ve two tables User and Friends

 User Table:                                Friends Table:
--------------------------                 -----------------------------------
 UserID    Name                            friendsid     friendname     userid
--------------------------                 -----------------------------------
 1097517748         User1                       3          friend1     536772587                     
 100003052455644    User2                       8          friend2     100003052455644
 536772587          User3                       8          friend3     1097517748
 4                  User4                       3          friend4     100003052455644

Now the friends table can contain different people who does or does not exist in the user table.In this example user 2 and 3 has friend with id 8 in common.I’ve got my guy but that guy id i.e 8 must be present in the user table also.This is only between userid 2 and 3.
I will be getting several userid and i’ve find mutual friends of all the friends that are referred with the guy who has posted the status.

I hope I’ve cleared it.If I get the idea for the query I can generate it dynamically using php for different number of referred friends.

So far I’ve written this query.

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748

But this is only bringing me mutual friends between two user.When I add a third guy lets say f3 and compare it with f1 it does not bring any result and another problem with this query is that it is not verifying that the mutual friend id exists in the user table or not.After adding the 3rd guy.

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
INNER JOIN friends AS f3 ON f1.friendsid = f3.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748
AND f3.userid = 536772587

This query does not bring any results.So I wanted to create a query that does all this.

EDIT:

Actually I am using facebook connect and I save all the users friends in my database when users login, the user table will contain only those users who are registered on my website.

  • 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-13T07:14:31+00:00Added an answer on June 13, 2026 at 7:14 am

    I finally figured it out. The query was

    SELECT DISTINCT(friendsid) FROM (
    SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
    INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
    and f1.userid = 100003052455644 and f2.userid =1097517748 
    INNER JOIN user ON  f2.friendsid = user.userid and f1.friendsid = user.userid 
    UNION ALL
    SELECT  (f1.friendsid) FROM friends AS f1 
    INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
    and f1.userid = 100003052455644  and f2.userid =536772587
    INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
    UNION ALL
    SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
    INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
    and f1.userid = 100003052455644  and f2.userid =694250830
    INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
    ) a
    

    This query will find the id’s of all the mutual friends between the user who posted the status and the users whom he refered.

    The user with id 100003052455644 is the person who posted status and all the others are his friends whom he referred.The DISTINCT will make sure that the id’s are unique and not repeated.This is for 3 persons he referred.We can add the UNION ALL part dynamically setting the id’s and this will help us create a dynamic query for retrieving mutual friends for different number of referred people.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a small application which will perform say 4 different, time consuming
I am creating a small utility which will help to rebuild install.php of WordPress.
I'm creating a small project which will manipulate some internal components of the project(big
I am creating small django application which holds some few questions (and answers for
I'm creating a small web page using jquery-ui-1.8 which is having a frameset and
I'm creating a small voting application. I need to get the images and their
I'm creating a small program which consists of a few source files on my
I'm creating a small web site that will mostly be used with modern smart
When creating WPF user controls I often find myself creating small converter classes when
I am creating a small website that allows a user to browse some of

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.