I have two columns in mysql one contains comma separated names of fields and other contains the values for those comma separated names
example
COL1 COL2
A,B,C,D 1,2,3,4
A,B,C 1,3,8
A has value 1 and so on ,
Now I want to insert these values in another table in their separate column
A B C D
1 2 3 4
1 3 8
I tried splitting the values using a function
DELIMITER $$
CREATE FUNCTION SPLIT_ST(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, ',');
END$$
SELECT SPLIT_ST(COL2, ',', 1) as A,
But the problem is some columns might not have all the fields as shown in example
I just want to do that
Insert into table (A,B,C,D) select from the table2
Please help