简体   繁体   中英

How can i convert a comma separated columns into junction table?

I am pretty new to mysql and this site. I got an old mysql database (100.000 entries) to migrate to our new system. This is the old table:

CUSTOMER

Customer_ID    Name    Categories
1              Bob     1,2
2              Phil    NULL
3              Ines    10,8
4              Carol   1
5              Rick    13,2

And i need the following structure:

CUSTOMER

Customer_ID    Name
1              Bob 
2              Phil   
3              Ines   
4              Carol 
5              Rick 

Category

Category_ID    Category_Name
1              Biker
2              Doctors
3              Teacher
...             ...
13              Drivers

CustomerHasCategory

Customer_ID    Category_ID  
1              1 
1              2  
3              10
3              8   
4              1
5              13 
5              2  

Thanks for any help.

I also had this problem but not in MySQL. I solved it with Python using the Pandas library. So, the exact steps I followed won't be useful for you. However, I'll show you the general idea behind the solution I used.

Below is image of the original column

在此处输入图像描述

First, I splitted the text into columns using the comas as the delimiter. 在此处输入图像描述

Next, I ' stacked ' the columns

在此处输入图像描述

Finally, I removed the artefact column(s). So, I have only the ID and the values columns. This creates a one-to-many relationship.

在此处输入图像描述

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