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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:59:21+00:00 2026-06-18T22:59:21+00:00

Here is the schema of my DB: id | user1 | user2 | status

  • 0

Here is the schema of my DB:

id | user1 | user2 | status  //status 1 means the user is a friend
 1     1       2      1
 2     3       1      1    
 3     1       4      1
 4     2       3      1
 5     4       2      1 

In the above example

  1. userId 1 has 3 friends 2,3,4
  2. userId 2 has 3 friends ie. 3,4,1
  3. userId 3 has 2 friends ie: 1,2
  4. userId 4 has 2 friends ie:1,2

I need to find mutual friends between 2 people.

Any Ideas,

  • 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-18T22:59:22+00:00Added an answer on June 18, 2026 at 10:59 pm

    I am not really sure if this is the best solution. But maybe something like this:

    SELECT
        *
    FROM
    (
        SELECT
            CASE WHEN t2.user1=1 THEN t2.user2 ELSE t2.user1 END AS userId
        FROM
            t AS t2
        WHERE 1 IN (t2.user1,t2.user2)
        AND t2.[status]=1
    ) as tbl
    WHERE EXISTS 
    (
        SELECT
            NULL
        FROM
            t
        WHERE 2 IN (t.user1,t.user2)
        AND tbl.userId=(CASE WHEN t.user1=2 THEN t.user2 ELSE t.user1 END)
        AND t.[status]=1
    )
    

    This gets the mutual friends from user 1 and 2. Which is 3,4

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

Sidebar

Related Questions

Here is my mysql table scheme; status table // has statusID, userID,date, subject, date
Here is my current setup: Database Role - MyDbRole Schema - MySchema User -
The Media Wiki database Schema, here , has taught me a lot. Cannot-understand: In
Here is my database schema: user *user_id *username *password *etc quiz_response *response_id *user_id *question_id
Here is a schema file, midi.xsd that defines a type, note , used to
So here's my schema (give or take): cmds.Add(@CREATE TABLE [Services] ([Id] INTEGER PRIMARY KEY,
I have xsd schema file which I can't change. Here is an excerpt that
c#/oop noob here. I am creating a datatable from the schema of a sql
Before anything else, here is the simplified schema ( with dummy records ) of
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified

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.