Duplicate / Copy records in the same MySQL table

前端 未结 9 1069
我在风中等你
我在风中等你 2020-12-04 06:27

I have been looking for a while now but I can not find an easy solution for my problem. I would like to duplicate a record in a table, but of course, the unique primary key

9条回答
  •  臣服心动
    2020-12-04 06:58

    The way that I usually go about it is using a temporary table. It's probably not computationally efficient but it seems to work ok! Here i am duplicating record 99 in its entirety, creating record 100.

    CREATE TEMPORARY TABLE tmp SELECT * FROM invoices WHERE id = 99;
    
    UPDATE tmp SET id=100 WHERE id = 99;
    
    INSERT INTO invoices SELECT * FROM tmp WHERE id = 100;
    

    Hope that works ok for you!

提交回复
热议问题