简体   繁体   中英

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 (ie 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM