Will Ashworth
Will Ashworth

Reputation: 1088

MySQL Union for two tables, then merge in a third table where matched?

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.

Upvotes: 0

Views: 6657

Answers (3)

Pooja Negi
Pooja Negi

Reputation: 1

select * into table_name from(
select * from #tb1 union select * from #tb2 )a

Upvotes: 0

Ronnis
Ronnis

Reputation: 12833

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);

Upvotes: 5

sikac
sikac

Reputation: 56

try something like this :)

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;

Upvotes: 0

Related Questions