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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:06:30+00:00 2026-06-05T12:06:30+00:00

Say I have users and that users can be friends. I have a table

  • 0

Say I have users and that users can be friends. I have a table of users and user data, and a table of friendships (connection id, user a id, user b id).

Is there a single query I could do in mySQL that would grab all of the users from the users table that are friends with user x? The fact that user x’s id could be in either the a slot or the b slot in the friendships table makes this more complicated than I can figure out.

Edit For Clarity:

I need all of the data in the users table for friends of the current user. So I need the user data for user b if the current user is user a in the friendships table and vice versa. I want to end up with all of the data for all of the current user’s friends (from the users table) based on the relationship in the friendship table.

  • 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-05T12:06:32+00:00Added an answer on June 5, 2026 at 12:06 pm

    Using SQL Fiddle you can do:

    SELECT u.name as friends
    FROM friendship AS f
    JOIN users AS u ON 
      ((u.user_id = f.user_a_id AND f.user_b_id = @user_id)
      or (u.user_id = f.user_b_id AND f.user_a_id = @user_id))
    where u.user_id is not null;
    

    In this @user_id is the id number of the user you want to find the friends for and the output are the names of the friends.

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

Sidebar

Related Questions

Let's say I have a subroutine/method that a user can call to test some
Let's say I have a table that has user_id, date, score, and every user
i have a system there user(sender) can write a note to friends(receivers), number of
Hi Sitepoint wizard people, Say we have an admin application that has multiple users
Let say, I have a web application that allows users to upload images and
Let's say I have a user that entered 12 links into the database but
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
Lets say you have a web app related Android app that requires the user
Say i have a table 'users', with 'user_id', 'name' and 'multiplier'. If I do
I have an app that needs to gain access to a user's friends' information.

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.