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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:05:40+00:00 2026-06-14T18:05:40+00:00

I have a table like this: user_id | wants_user_id | state And I want

  • 0

I have a table like this:

user_id | wants_user_id | state

And I want to select rows which are mutual and have a state column with value “yes” or “maybe”.

Example data:

Row 1: 1 | 2 | "yes"
Row 2: 2 | 1 | "maybe"
Row 3: 3 | 2 | "yes"

In this case I would like to select row 1 and 2 and on wants_user_id (second column) I would like to join user data from table users, but I can’t figure out the sql command.

Thank you for your help.

  • 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-14T18:05:41+00:00Added an answer on June 14, 2026 at 6:05 pm

    Use a self-join to get matching rows (I’m assuming “mutual rows” here means users that want each other):

    SELECT *
    FROM myTable a
    INNER JOIN myTable b ON a.user_id = b.wants_user_id
        AND b.user_id = a.wants_user_id
    INNER JOIN users ua ON a.user_id = ua.id
    INNER JOIN users ub ON b.user_id = ub.id
    WHERE a.state IN ('yes', 'maybe')
    AND b.state IN ('yes', 'maybe')
    

    In this case, you will get two “duplicate” rows, one for 1<->2 and one for 2<->1. To eliminate this, you can add a AND b.user_id > a.user_id to the join condition – this will select only one row of each pair.

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

Sidebar

Related Questions

I have a couple of tables which look like this Table 1 user_id |
I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
I have a table like this: a | user_id ----------+------------- 0.1133 | 2312882332 4.3293
I have a table in my database like this: [id] [uniqueidentifier] NOT NULL, [user_id]
I have a table which stores test results like this: user | score |
I have two tables, stats and stat_log. The stat log is like this: user_id,stat_id,value,registered
Suppose you have a table like this: id user score It's a dump of
I have a user table like this FIRSTNAME | LASTNAME | ID | --------------------------------
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...

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.