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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:01:39+00:00 2026-05-29T06:01:39+00:00

I have two tables inside a database. One stores unique userNames and a unique

  • 0

I have two tables inside a database. One stores unique userNames and a unique id and the other stores which users from the previous table are “friends”: ex:

table users:
id   | username
---------------
100  |   aaa
200  |   bbb
300  |   ccc

table friends:
id | user  | friend
-------------------
1  | 100   | 200
2  | 300   | 100
3  | 300   | 200 

Like in the above example, user 100 is friends with 300 and also 200.
I’d like to display a list containing all of users 100 friends. Keep in mind he can appear in the “friends” table on both columns (user and friend). Can’t figure out how the query should look like. Everything i try, it duplicates rows and whatnot.

I know it’s trivial, but i’m new at this.

  • 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-29T06:01:40+00:00Added an answer on May 29, 2026 at 6:01 am

    This will work with no duplicate

    SELECT distinct id FROM(
    SELECT friend as id FROM friends
    WHERE user = 100
    UNION ALL
    SELECT user as id FROM friends
    WHERE friend = 100) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with hierarchyid fields, one of which is a staging table
Let's say that I have two tables in my database: users countries I have
I have an asp.net website that stores events inside a database table. Then I
Let's say I have two tables in a database: projects and users. I create
I have two tables in my database as following: Employee Table: Username, Name, Job,
I have two tables in my application. The first one, Sections, stores content for
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I am just begining php and mysql. I have two queries from my database.

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.