By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Select distinct from multiple columns into one column

P: 2
Hello,
The problem is that I want to select distinct values from multiple columns of one table and want to select one column.
Say column1 has values: "first","second","third"
and column2 has values: "second","fifth","first"

I want to select unique from the above mentioned two columns so that result is like this:-
colname: "first","second","third","fifth"

Please suggest. Thanks in advance
Jul 20 '11 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,349
The data should have been normalized in the first place.

I don't know if mysql supports unpivot but unpivot the data if you can. If you can't, you can use a union query to get the data in one column.

After normalizing the data, you can run your distinct.
Jul 20 '11 #2

P: 2
Thank you for posting Rabbit. Yes, it solved my problem. Thank you very much
I used :-
( select column1 as a from tbl_customer )
UNION Distinct
( select column2 as a from tbl_customer )


Though problem has been solved but I am thinking that in this statement I need to put select statement for that many times as many columns I have from the same table. Because I am selecting the table again and again, I think itmay take more time in executing.. there should be some other good way, something like
select ( column1 union column2 ) as result from table

Thanks
Jul 22 '11 #3

Rabbit
Expert Mod 10K+
P: 12,349
There is no other way. That is one of the drawbacks of using non-normalized data.
Jul 22 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.