Answer the question
In order to leave comments, you need to log in
How to duplicate a row in mysql?
Hello. The situation is as follows, there is a table with records, there is a field in which there can be either one or several values separated by commas (Group1, group2, group3, etc.).
How can I make a record where several values are cloned by the number of these values, that is, a record for Group1, a record for Group2, etc.
Answer the question
In order to leave comments, you need to log in
You can do it, but most likely you have problems with the data model. If such a task arises, then the groups are placed in a separate table and a connection is made with the original table.
If you need it with such a model, then here is the request:
SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(groups, ',', g.n), ',', -1) groupName
FROM (
SELECT @n:[email protected]+1 n FROM
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) s0,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) s1,
(SELECT @n:=0) sg
) g
JOIN Item ON CHAR_LENGTH(groups)-CHAR_LENGTH(REPLACE(groups, ',', '')) >= g.n-1
ORDER BY id, n
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question