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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:47:53+00:00 2026-06-04T11:47:53+00:00

I am trying to learn about MYSQL Joins but am having trouble figuring this

  • 0

I am trying to learn about MYSQL Joins but am having trouble figuring this one out. I want to get all rows in table1 where the userID is in a relationship with the given user.

table2 contains user relationships so the userID can either be in iduser1 field or iduser2 field
I have the following query that gives me the correct results:

set @userID = 91;
SELECT * FROM table1
    WHERE (iduser IN (SELECT iduser1 FROM table2 WHERE iduser2 = @userID)
        OR (iduser IN (SELECT iduser2 FROM table2 WHERE iduser1 = @userID)))

table1: 
    iduser FK

table2:
    iduser1 FK
    iduser2 FK

I have been told that nested queries in MySQL have a bad reputation when it comes to performance and I am sure I could probably do this same query using JOINS somehow but I just cant figure it out especially as there is an OR statement because the table1.iduser can be in either table2.iduser1 OR table2.iduser2

How can I join the same table twice?

  • 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-04T11:47:54+00:00Added an answer on June 4, 2026 at 11:47 am

    Given this expression:

    set @userID = 91;
    SELECT * FROM table1
        WHERE (iduser IN (SELECT iduser1 FROM table2 WHERE iduser2 = @userID)
            OR (iduser IN (SELECT iduser2 FROM table2 WHERE iduser1 = @userID)))
    

    Using EXISTS, you can reduce that two subqueries into following:

    set @userID = 91;
    SELECT * FROM table1 x
    WHERE EXISTS(
        select * from table2 z
        where (z.user2 = @userID and iduser1 = x.iduser)
           OR (z.user1 = @userID and iduser2 = x.iduser)
    )
    

    The OR might slowdown your query, it doesn’t necessarily short-circuit in RDBMS. In that case, you should try to short-circuit it by using CASE WHEN. I had a query that took 5 seconds to execute when using OR, but took sub-zero second when I translated it to CASE WHEN:

    set @userID = 91;
    SELECT * FROM table1 x
    WHERE EXISTS(
        select * from table2 z
        where 
           CASE WHEN z.user2 = @userID THEN
               IF(z.iduser1 = x.iduser, 1, 0)
           CASE WHEN z.user1 = @userID THEN 
               IF(z.iduser2 = x.iduser, 1, 0)
           END = 1
    )
    

    Boolean expression automatically cast to integer(either 0(false) or 1(true)), so you can also do it like this:

    set @userID = 91;
    SELECT * FROM table1 x
    WHERE EXISTS(
        select * from table2 z
        where 
           CASE WHEN z.user2 = @userID THEN
               z.iduser1 = x.iduser
           CASE WHEN z.user1 = @userID THEN 
               z.iduser2 = x.iduser
           END = 1
    )
    

    Or this:

    set @userID = 91;
    SELECT * FROM table1 x
    WHERE EXISTS(
        select * from table2 z
        where 
           CASE WHEN z.user2 = @userID THEN
               z.iduser1 = x.iduser
           CASE WHEN z.user1 = @userID THEN 
               z.iduser2 = x.iduser
           END 
    )
    

    If you are not using MySQL, you shall do it like this:

    set @userID = 91;
    SELECT * FROM table1 x
    WHERE EXISTS(
        select * from table2 z
        where 
           CASE WHEN z.user2 = @userID THEN
               CASE WHEN z.iduser1 = x.iduser THEN 1 END
           CASE WHEN z.user1 = @userID THEN 
               CASE WHEN z.iduser2 = x.iduser THEN 1 END
           END = 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn some javascript and i'm having trouble figuring out why my
I'm trying to learn about trees by implementing one from scratch. In this case
I am trying to learn about the Jini API in java, but can't get
I am somewhat new to jQuery but have been having fun trying learn about
I've been trying to learn about metaclasses in Python. I get the main idea,
I'm trying to learn about this feature of javascript I keep seeing in code,
I am trying to learn about all possible conditionals that can be used on
I found this script on about.com which I'm trying to learn from on how
I found this script on about.com which I'm trying to learn from on how
I've been trying to learn about events/delegates, but am confused about the relationship between

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.