2

I need to duplicate a row multiple times in the same table.

I have a table like this

Please see screenshot

I want to duplicate the second row multiple times but with increment to ID.

How do I achieve this?

Thanks a lot in advance!

Simon Jonnes
  • 21
  • 1
  • 3
  • Write n insert statements or write 1 insert statement in a loop in a procedure. – P.Salmon Oct 13 '17 at 17:22
  • 2
    Possible duplicate of [MySQL: How to copy rows, but change a few fields?](https://stackoverflow.com/questions/2783150/mysql-how-to-copy-rows-but-change-a-few-fields) – Bill the Lizard Oct 13 '17 at 17:22

1 Answers1

3

Add 'auto_increment' in property for id and:

INSERT INTO
    table_name (user_id, value, time, category)
SELECT
    user_id, value, time, category
FROM
    table_name 
WHERE
    id = 330; 

if need modification table:

ALTER TABLE table_name MODIFY id INT AUTO_INCREMENT PRIMARY KEY
mscdeveloper
  • 2,749
  • 1
  • 10
  • 17