Select distinct from multiple columns into one column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sunny
    New Member
    • Jul 2011
    • 2

    Select distinct from multiple columns into one column

    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","fift h"

    Please suggest. Thanks in advance
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • Sunny
      New Member
      • Jul 2011
      • 2

      #3
      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

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        There is no other way. That is one of the drawbacks of using non-normalized data.

        Comment

        Working...