I think you'll need to use a stored procedure, check these other posts for (possibly) helpful info:
- split keywords for post php mysql
- MySQL procedure to load data from staging table to other tables. Need to split up multivalue field in the process
- How to expand comma-separated field into multiple rows in MySQL
Or you could try to do it with a function and a group_concat():
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
And then the select query would be:
SELECT
tmp.id,
SPLIT_STR(tmp.mobs, ',', 1) as mob1,
SPLIT_STR(tmp.mobs, ',', 2) as mob2,
SPLIT_STR(tmp.mobs, ',', 3) as mob3,
SPLIT_STR(tmp.mobs, ',', 4) as mob4
FROM (
SELECT
id,
GROUP_CONCAT(mob1) as mobs
FROM person
GROUP BY name
) tmp
Of course, then you'd have to integrate that into an UPDATE
statement, but I'll let you try that on your own. (btw, got the FUNCTION
from here).