How to copy a row and insert in same table with a autoincrement field in MySQL?

后端 未结 13 1073
旧时难觅i
旧时难觅i 2020-11-29 15:55

In MySQL I am trying to copy a row with an autoincrement column ID=1 and insert the data into same table as a new row with

13条回答
  •  清歌不尽
    2020-11-29 16:41

    I was looking for the same feature but I don't use MySQL. I wanted to copy ALL the fields except of course the primary key (id). This was a one shot query, not to be used in any script or code.

    I found my way around with PL/SQL but I'm sure any other SQL IDE would do. I did a basic

    SELECT * 
    FROM mytable 
    WHERE id=42;
    

    Then export it to a SQL file where I could find the

    INSERT INTO table (col1, col2, col3, ... , col42) 
    VALUES (1, 2, 3, ..., 42);
    

    I just edited it and used it :

    INSERT INTO table (col1, col2, col3, ... , col42) 
    VALUES (mysequence.nextval, 2, 3, ..., 42);
    

提交回复
热议问题