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

后端 未结 26 2320
-上瘾入骨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:20

    I might be late in this, but I have a similar solution which has worked for me.

     INSERT INTO `orders` SELECT MAX(`order_id`)+1,`container_id`, `order_date`, `receive_date`, `timestamp` FROM `orders` WHERE `order_id` = 1
    

    This way I don't need to create a temporary table and etc. As the row is copied in the same table the Max(PK)+1 function can be used easily.

    I came looking for the solution of this question (had forgotten the syntax) and I ended up making my own query. Funny how things work out some times.

    Regards

提交回复
热议问题