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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:14:32+00:00 2026-06-04T12:14:32+00:00

i have a user table id , username 22 , max 33 , jack

  • 0

i have a user table

 id , username
 22 , max
 33 , jack
 44 , joe

And friends table

id , u1 , u2 , status , reciver

For increasing the speed on reading the friends i’ve decided to add two row to the table for each friendship
so it goes like this

id , u1 , u2 , status , reciver

 1 , 22 , 33 ,  1     ,  22
 2 , 33 , 22 ,  1     ,  22


 3 , 22 , 44 ,  1     ,  22
 4 , 44 , 22 ,  1     ,  22

 5 , 22 , 55 ,  1     ,  22
 6 , 55 , 22 ,  1     ,  22

Now i want to get each user friends and count the mutual friends between these two like facebook

Here is my friends list query :

         $profile_id = $current_user->id;

         $res = $db->query("
            select 
            u.id as firend_id, 
            u.username  as firend_name,
            f.status    as firendship_status,
            COUNT(f3.u2)  as mutual  

            from friends f      
              JOIN users  u on f.u2 = u.id
              LEFT JOIN friends f2 on f.u2 = f2.u1 and f2.u2 != $profile_id
              LEFT JOIN friends f3 on f3.u1 = $profile_id AND f3.u2=f2.u2     
            WHERE  f.u1 = $profile_id
            group by f2.1
           ");

        echo 'friends list : '
        foreach ($res->result() as $r ){

          echo $r->firend_name;
          echo $r->mutual.' friends';

        }

so i check the owner of profile id ($profile_id) against friends.u1 and
join the friends.u2(founded friend) to the users table to get his username and id

the problem is in counting the mutual friends

I join the friends table with it self
on each founded friend

It seems ok on the paper but it returns some weird results

Like if the users has 3 friends and there is no mutual friends it return only one of the 3 friends
!

And if there is mutual friends it shows all of the friends but the mutual friends number is wrong on most cases

there is a problem with group by statement

  • 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-04T12:14:34+00:00Added an answer on June 4, 2026 at 12:14 pm

    You can’t do this with only 2 friends tables, you’ll need a third:

    • Friends1: select friends of user $profile_id (as you have)
    • Friends2: select the friends of the friend of $profile_id (as you have)
    • Friends3: work out the mutual friends between the friends of the the
      friend of $profile_id and the friends of $profile_id

    I’ve not tested the following and it works for my sample data – just double check on a larger sample:

            select  
            u.id as firend_id,  
            u.username  as firend_name, 
            f.status    as firendship_status, 
            COUNT(f3.u2)  as mutual 
    
            from friends f       
              JOIN users  u on f.u2 = u.id 
              LEFT JOIN friends f2 on f.u2 = f2.u1 and f2.u2 != $profile_id 
              LEFT JOIN friends f3 on f3.u1 = $profile_id AND f3.u2=f2.u2
    
            WHERE  f.u1 = $profile_id 
            group by f.u2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of users with three columns. Username Accepted Rejected User 1
I have the following table: Table: UserName Userid User UserUpdate 1 Dan 1/1/2005 1
I have a User table, which is having Userid(primary key), Username, Password There is
We have a user table, every user has an unique email and username. We
I have an user table like this:- guid | username | password | firstname
I have a user table with id, username and food_id columns. The idea is
I have a user table(id, username, ...), and I have a task table, this
In MySql database I have table user user_id | user_name --------+---------- 1 | Joe
I have 2 tables - one storing user information (id, username, password) and the
I have a User table that has all of their avatars saved in an

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.