KPM
KPM

Reputation: 747

MySQL: Combining multiple columns from Table 1 and inserting into 1 column in Table 2

I've been trying to figure this out, but can't seem to come up with a simple solution.

Say for instance I have a table that has similar data throughout 3 columns (i.e. different types of activities spanning 3 columns) but I want to have those three columns inserted into a separate table (Table2) so I can keep the like data together and perform a JOIN to match it with its respective data in Table1.

I'm not talking about performing a CONCAT or CONCAT_WS, but moving those three columns from Table1 into one column in Table2, each item with its own row.

Is there a way to do this through a query without having to manually insert each entry into Table2?

Thank you in advance!

Upvotes: 0

Views: 615

Answers (1)

Dan Bracuk
Dan Bracuk

Reputation: 20804

It might be as simple as:

insert into table2
(field)
select column1 from table1
union
select column2 from table1
union
select column3 from table1

But, before you do this, decide what you want to do if two columns in table1 have the same value.

Upvotes: 1

Related Questions