MySQL: choose Distinct from two different tables?

I have two different tables, each of which has a column called product_type . How can I get the DISTINCT values ​​for product_type for both tables? Just to clarify if both tables have a diamond product_type, I want it to return once. Basically, it’s as if both tables where they were combined, and I selected a separate product_type from it.

Thanks!!

+7
source share
3 answers

Use distinctive from a subquery that contains a union

select distinct product_type from ( select product_type from table 1 union select product_type from table 2 ) t 
+18
source

Use different and combine:

 select distinct product_type from table1 union select distinct product_type from table2 

Merge removes duplicates when merging results.

+5
source

Note that the UNION clause returns unique field values ​​when you want it to return ALL values ​​that you should use UNION ALL ...

 select product_type from table_a union product_type from table_b 
+2
source

Source: https://habr.com/ru/post/1286304/


All Articles