A
A
agsidorov2014-10-03 14:24:19
MySQL
agsidorov, 2014-10-03 14:24:19

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

1 answer(s)
S
Sergey Senkevich, 2014-10-03
@agsidorov

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

Here is a link to a working example on sqlfiddle . This example assumes that each entry has no more than 100 groups. If they are always less than 10, then table s1 can be removed from the query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question