In MySQL, can I copy one row to insert into the same table?

后端 未结 26 2365
-上瘾入骨i
-上瘾入骨i 2020-11-27 09:56
insert into table select * from table where primarykey=1

I just want to copy one row to insert into the same table (i.e., I want to duplicate an ex

26条回答
  •  没有蜡笔的小新
    2020-11-27 10:26

    clone row with update fields and auto increment value

    CREATE TEMPORARY TABLE `temp` SELECT * FROM `testing` WHERE id = 14;
    
    UPDATE `temp` SET id = (SELECT id FROM testing ORDER by id DESC LIMIT 1
     )+1, user_id = 252 ,policy_no = "mysffffdd12" where id = 14;
    
    INSERT INTO `testing` SELECT * FROM `temp`;
    
    DROP TEMPORARY TABLE IF EXISTS `temp`;
    

提交回复
热议问题