473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Select distinct from multiple columns into one column

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
3 5244
Rabbit
12,516 Expert Mod 8TB
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
Sunny
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
12,516 Expert Mod 8TB
There is no other way. That is one of the drawbacks of using non-normalized data.
Jul 22 '11 #4

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

Similar topics

3
by: Jay | last post by:
I have a table with a varchar(255) column, it has 111,000 rows. When I do a select distinct on that column it takes 16 seconds and returns about 25 distinct values, I'd like it to take much less...
2
by: mfyahya | last post by:
I have two tables, both containing an 'authors' column. Is there a way to get a unique list of authors from the two tables? I tried SELECT DISTINCT `authors` from `table1`, `table2`; but I got an...
3
by: gregory.sharrow | last post by:
I need to secure a datawarehouse table at the row level based on 1 to many keys on that table. A user should only see the rows they have access to. I need to be able to figure out which rows they...
2
by: C L Humphreys | last post by:
Is there a way of specifying how select distinct works? In other words I don't want it to work on every column, just a selection of them. What I'm really looking for is ways of removing...
2
by: Frank van Vugt | last post by:
Hi, Not exactly a showstopper, but I noticed this behaviour: db=# create table f1 (id int, value int); CREATE TABLE db=# insert into f1 select 1 as id, null; INSERT 25456306 1
6
by: zaphod | last post by:
I need to select unique combinations of 4 columns from one table and insert them into a new table but I can't think of any way of finding unique combinations of more than 1 column since SELECT...
7
by: beary | last post by:
I'll try to explain the problem, but it may not make sense. I have a mysql table with columns like id,title,speaker,day,month,year, topic etc. At present, my syntax is select * from files order...
17
by: ukchat | last post by:
I.m creating a dynamic query to pull out workbooks from my database the table structure is below. Table: curricworkbooks Columns: ID, curric, assessment, topic, workbook, filename Example data...
2
omerbutt
by: omerbutt | last post by:
hi there i have to select 7 columns from table1, 1 column from table2, and 1 column from tables3 and show the result but i am not getting it right the main thing that i am trying to achieve is that i...
4
by: omnittha | last post by:
I would like to pull unique records of a SINGLE column but not the other related columns. When I use DISTINCT, it pulls up all the records which are unique across the columns. Here is the code.......
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.