The database I created 15 years ago was poorly designed. I have a varchar column containing comma separated strings: '5,8,13'. The string does not contain spaces and trailing commas, but the number of values is variable. What I want to do is create a new table and move these values paired with the row's ID into the new table. Assuming the above value comes from row with ID 7, the result will be 3 rows:
[ {7, 5} {7, 8} {7, 13} ]
View the existing data set, the value of this column ranges from 1 to 6. I found a thread that shows how to use the split string function: Split value from one field to two
But frankly, I don't know how to translate this into a single insert operation. If this is not a simple task, then I will just write a PHP script, even if it will have many insert statements.
Further explanation, here are two tables:
create table Table_A( id int auto_increment primary key, platforms varchar(255) ) create table Table_B( id int auto_increment primary key, platform int not null )
Table_A is the existing data, where the data in the platforms column is the comma-separated numbers "3,45," or "56,4". These numbers point to an index in another table. Yes, I know it's a bad design, luckily I've learned a better way since then. I want to parse the numbers from Table_A's platform string and insert them into Table_B along with the Table_A row's ID.
Here is a small query to achieve this function. It only splits 6 values from CSV. If there are more values in a row you have to change UNION ALL
The results are stored in singletable.