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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:48:43+00:00 2026-05-27T09:48:43+00:00

So I apologize if this has come up before, but I couldn’t find a

  • 0

So I apologize if this has come up before, but I couldn’t find a solid answer.

I am trying to get a single column of ids from a set of 3 columns in my table joined with another table.

The select statement just to get a single column would be like this:

SELECT id FROM TableA JOIN TableB USING (key1, key2) WHERE someValue = X;

Now as I understand it I can do unions to get these like so:

SELECT spec_id1 AS id FROM TableA JOIN TableB USING (key1, key2) WHERE someValue1 = X
UNION
SELECT spec_id2 AS id FROM TableA JOIN TableB USING (key1, key2) WHERE someValue2 = Y
UNION
SELECT spec_id3 AS id FROM TableA JOIN TableB USING (key1, key2) WHERE someValue3 = Z

Is there a way to reuse the TableA JOIN TableB USING (key1, key2) so it isn’t joined for each select? I also have a WHERE condition that applies to all of the selects that is not shown above. Could I possibly reuse something like:

FROM TableA JOIN TableB USING (key1, key2) WHERE someOtherValue = W

I am trying to do this in PHP using mysqli. My original instinct was to use a temp table and query that, but I’m not sure how to handle that in mysqli. If I can use the table for multiple following queries that would be doubly ideal as there is extra data in the joined tables that could be used later.

Sorry if this is naive, but I’m not really a big web developer. Thanks in advance.

EDIT:
I am looking into temporary tables, and it seems like that might be a good way around it. I’d still like some confirmation on whether that’s a good idea or not before doing anything with it.

  • 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-27T09:48:43+00:00Added an answer on May 27, 2026 at 9:48 am

    There is no shortcut – not without changing the tables’ design. Your approach is solid and you should not worry about having 3 joins in your query.

    If the 3 lists of spec_ids are known to not have any overlapping values, then you could improve performance by changing UNION to UNION ALL.


    One possible rewriting, without changing the overall UNION plan, is to change joins to EXISTS subqueries:

        SELECT spec_id1 AS id 
        FROM TableA 
        WHERE EXISTS
              ( SELECT * 
                FROM TableB 
                WHERE (TableB.key1, TableB.key2) = (TableA.key1, TableA.key2)
                  AND someValue1 = X
              )
    UNION                --- or UNION ALL
        ...
    UNION
        ...
    

    or – if the spec_ids are all included in another BaseTable – you can have something like this which might yield a better execution plan:

    SELECT id
    FROM BaseTable
    WHERE EXISTS
          ( SELECT * AS id 
            FROM TableA 
              JOIN TableB 
                USING (key1, key2) 
            WHERE someValue1
              AND TableA.spec_id1 = BaseTable.id
          )
        OR EXISTS
           ( 
           ...
           )
        OR ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize if this has been asked before but I am trying to parse
I apologise if this has been asked before but I can't find the info
I apologize if this has already been asked a different way but I couldn't
I apologize if this has been asked before, but I haven't quite found the
(apologies if this has come up before, I've searched but not found anything for
If this has been asked before, I apologize but this is kinda of a
I apologize if this has been asked before, but I believe that for me,
I apologize if this question has been asked, but I can't find a question
I apologize because I know this has been covered over and over again, but
Hopefully no one has asked this question - didn't see it, but I apologize

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.