tags:

views:

215

answers:

3

I have 2 different tables that each have a column called product_type. How can I get the DISTINCT values of product_type across both tables? Just to clarify, if both tables have a product_type of "diamond" I only want it returned once. Basically as if both tables where combined and I selected distinct product_type from it.

Thanks!!

+1  A: 

Note that the UNION clause returns unique values of the field, when you want it to return ALL the values you must use UNION ALL...

select product_type from table_a
union
product_type from table_b
tekBlues
+4  A: 

Use a distinct with a subquery which contains a union

select distinct product_type from (
    select product_type from table 1
    union 
    select procut_type from table 2
) t
AlbertEin
Thanks, one question.. is that "t" supposed to be at the end? what is it for? Thanks!
John Isaacks
the T is the alias to the subquery, so, you can say t.produc_type and it'll mean "the field product_type from the subquery called T"
AlbertEin
Do you need the outer query? I believe the UNION will only return distinct rows. (UNION ALL would require the outer query)
James Moore
A: 

Use distinct and union:

select distinct product_type from table1
union
select distinct product_type from table2

The union will remove duplicates when combining the results.

Guffa