Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only distinct values from two columns from a table

If I have a table such as

1 A

1 B

1 A

1 B

2 C

2 C

And I want to select distinct from the two columns so that I would get

1 

2 

A

B

C

How can I word my query? Is the only way to concatenate the columns and wrap them around a distinct function operator?

like image 683
usr021986 Avatar asked May 14 '13 06:05

usr021986


1 Answers

You could use a union to create a table of all values from both columns:

select  col1 as BothColumns
from    YourTable
union
select  col2
from    YourTable

Unlike union all, union removes duplicates, even if they come from the same side of the union.

like image 139
Andomar Avatar answered Sep 29 '22 05:09

Andomar