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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:05:12+00:00 2026-05-20T01:05:12+00:00

I have three MySQL tables that I’m trying to query into a single result.

  • 0

I have three MySQL tables that I’m trying to query into a single result. As close as I’ve gotten this, I think it’s possible, but I’ve hit a wall in getting the last part working.

Basically I have two tables (table_one and table_two) which I’m trying to UNION DISTINCT, which works perfectly. It’s when I try to bring in the third table that it all blows up and decides to return nothing. I’m sure it’s user error 🙂

This code may not have everything in the right place for how I’m trying to do it, so maybe I just need a little nudge in the right direction.

SELECT
    part_code,
    name
FROM
    table_three

WHERE
    table_three.part_code = (

    SELECT
        part_code
    FROM
        table_one

    UNION DISTINCT
    SELECT
        part_code
    FROM
        table_two
)

ORDER BY
    name ASC

I appreciate any direction someone can offer.

  • 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-20T01:05:13+00:00Added an answer on May 20, 2026 at 1:05 am
    WHERE
        table_three.part_code IN(
                              ^^
    

    Edit
    Here are some alternatives that satisfies: Gief all rows in Table 3 such that the part code exists in either Table 1 or Table 2.

    select t3.part_code
          ,t3.name
      from table_three t3
     where part_code in(select t1.part_code from table_one t1)
        or part_code in(select t2.part_code from table_two t2);
    

    Derived table with union

    select t3.part_code
          ,t3.name
      from table_three t3
      join (select part_code from table_one 
             union
            select part_code from table_two
           ) t12
         on(t3.part_code = t12.part_code);
    

    Inner join with union

    select t3.part_code
          ,t3.name
      from table_three t3
      join table_one   t1 on(t3.part_code = t1.part_code)
    union   
    select t3.part_code
          ,t3.name
      from table_three t3
      join table_two   t2 on(t3.part_code = t2.part_code);  
    

    Bonus. I have no idea why I did this.

    select t3.part_code
          ,t3.name
      from table_three t3
      left join (select distinct part_code 
                          from table_one) t1 on(t3.part_code = t1.part_code)
      left join (select distinct part_code 
                          from table_two) t2 on(t3.part_code = t2.part_code)
     where t3.part_code = t1.part_code
        or t3.part_code = t2.part_code;
    

    Let me know how they work out.

    Edit 2.
    Ok, try the following. It should produce the union of tables T1 and T2. Then for each row, it will pick the name from T3 if such a part code can be found.

    If part_code is a key in all tables, you can do a UNION ALL instead.

    select T12.part_code
          ,coalesce(T3.name, T12.name) as name
      from (select part_code, name from table_one T1 union   
            select part_code, name from table_two T2
           ) T12
      left join table_three T3 on(T1.part_code = T3.part_code);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three MySQL queries I need to combine into a single query using
I have a MySQL Left Join problem. I have three tables which I'm trying
i have three tables i would like to link in this one query. The
I have three tables in MySQL that are related, yet not technically linked to
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I have a MySql database with three tables I need to combine in a
I have three tables in a MySQL database used in a music library application:
I have three tables: stories , story_types , and comments The following query retrieves
I have two tables in MySQL that I'm comparing with the following attributes: tbl_fac
I have a couple of tables in MySQL that I'm working with that do

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.