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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:31:48+00:00 2026-05-16T15:31:48+00:00

I need help with a MySQL Query. The Output of the query should look

  • 0

I need help with a MySQL Query. The Output of the query should look like this

User1       User2
xxx1        xxx2
xxx3        xxx1

but not

User1       User2
xxx1        xxx2
xxx2        xxx1

and so on.

I need all “friendships” between different users. A friendship exists i.e. when

UserId 8 exists in Column User1_id  
UserId 4 exists in Column User2_id  
**AND**
UserId 8 exists in Column User2_id  
UserId 4 exists in Column User1_id 

Friendship Table

+----------+----------+
| User1_id | User2_id |
+----------+----------+
|        8 |        4 | 
|        4 |        8 | 
|       29 |        4 | 
|        4 |       10 | 
|       10 |        4 | 
|        8 |       37 | 
|        4 |       29 | 
|       37 |        8 | 
|       37 |        4 | 
|       29 |        8 | 
|        4 |       37 | 
|        8 |       10 | 
|        8 |       29 | 
|        4 |       40 | 
|       40 |        4 | 
|       40 |       29 | 
|       29 |       40 | 
+----------+----------+

User Table

+----+-----------------------------+
| id | username                    |
+----+-----------------------------+
|  4 | hhessel                     | 
|  8 | xxx1                        | 
| 10 | xxx2                        |  
| 29 | xxx3                        | 
| 40 | xxx4                        | 
| 37 | xxx5                        | 
| 39 | xxx6                        | 
+----+-----------------------------+
  • 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-16T15:31:49+00:00Added an answer on May 16, 2026 at 3:31 pm

    Use:

    SELECT a.user1_id, 
           a.user2_id
      FROM FRIENDSHIP a
      JOIN FRIENDSHIP b ON b.user2_id = a.user1_id
                       AND b.user1_id = a.user2_id
                       AND b.user1_id > a.user1_id
    

    You’ll have to reverse the user1_id comparison if you want the column values reversed:

    AND b.user1_id < a.user1_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that produces this output ordered like this: Instead I'd
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I need help regarding array insertion into mysql table. I have an array when
I am newbee with PHP and MySQL and need help... I have two tables
I need some help with some PHP and MySQL code. At the moment I
I'm new to MySQL so I really need some help with an issue I'm
I'm a noob with mysql and php, and need some help :) I have
I don't know MySQL very well so I need a bit of help with
Need help with a query that I wrote: I have three tables Company id
Using C#.Net and MYSQL I want to use a CASE Statement like SQL Query

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.