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

  • Home
  • SEARCH
  • 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 6542867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:16:42+00:00 2026-05-25T11:16:42+00:00

I have a database table called users with a primary key of user_id for

  • 0

I have a database table called users with a primary key of user_id for each user.

I also have a table called friends with two fields, user_id and friend_user_id.

The user_id field is always the lowest of the two user_id’s in order to avoid duplicate entries.

Say I have two users in mind, (lets say user id 1 and user id 4 although they could be anything).

How would I return all rows from the users table for users that are friends with user 1 and user 4 (i.e mutual friends)?

  • 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-05-25T11:16:42+00:00Added an answer on May 25, 2026 at 11:16 am

    I will give you the recipe:

    1. Find all friends of user 1
    2. Find all friends of user 2
    3. Intersect them and the result will be the mutual friends.

    Much like this:

    enter image description here

    UPDATE: Here’s the query:

    select f.friend_user_id  from friends f where f.friend_user_id in (
       select friend_user_id from friends where user_id=<id_of_user_a>)
    and f.user_id=<id_of_user_b>
    

    The ids returned by above query will be the id of all the users that are mutual friends of user_a and user_b. If you want to get all the details (name, etc) about those users, then do this:

       select f.friend_user_id,u.*  from friends f inner join users u 
       on u.user_id=f.friend_user_id
       where f.friend_user_id in (
       select friend_user_id from friends where user_id=<id_of_user_a>)
       and f.user_id=<id_of_user_b>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table and one of the fields (not the primary key)
I have a database table called users This table has two columns (that are
I have a table called Users in my database. Let's assume that User has
Say I have a database called Poll and have these two table structures. poll
So, I have a database (With a table called users) that let's users connect
I have a database table called Posts which stores all the information regarding an
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
I'm binding a gridview dynamically from a table in my database I have called
Lets say I have a table in a sql server 2000 database called TransactionType:
I have two tables in my database, called ratings and movies . Ratings: |

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.