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

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

    This solution showed above works perfect also for selected rows. For example I am creating demonstration rows for my nice2work project, and this works perfect.

    CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
    UPDATE tmptable SET id = 0;
    UPDATE some fields I need to change
    INSERT INTO myTable SELECT * FROM tmptable;
    DROP TABLE tmptable;
    
    //  You can use this same also directly into your code like (PHP Style)
    $sql = "CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
    UPDATE tmptable SET id = 0;
    UPDATE some fields I need to change
    INSERT INTO myTable SELECT * FROM tmptable;DROP TABLE tmptable;";
    
    0 讨论(0)
  • 2020-11-27 10:07

    Update 07/07/2014 - The answer based on my answer, by Grim..., is a better solution as it improves on my solution below, so I'd suggest using that.

    You can do this without listing all the columns with the following syntax:

    CREATE TEMPORARY TABLE tmptable SELECT * FROM table WHERE primarykey = 1;
    UPDATE tmptable SET primarykey = 2 WHERE primarykey = 1;
    INSERT INTO table SELECT * FROM tmptable WHERE primarykey = 2;
    

    You may decide to change the primary key in another way.

    0 讨论(0)
  • 2020-11-27 10:07

    You almost had it with the your first query you just need to specify the columns, that way you can exclude your primary key in the insert which will enact the auto-increment you likely have on the table to automatically create a new primary key for the entry.

    For example change this:

    insert into table select * from table where primarykey=1
    

    To this:

    INSERT INTO table (col1, col2, col3) 
    SELECT col1, col2, col3 
    FROM table 
    WHERE primarykey = 1
    

    Just don't include the primarykey column in either the column list for the INSERT or for the SELECT portions of the query.

    0 讨论(0)
  • 2020-11-27 10:08

    I used Grim's technique with a little change: If someone looking for this query is because can't do a simple query due to primary key problem:

    INSERT INTO table SELECT * FROM table WHERE primakey=1;
    

    With my MySql install 5.6.26, key isn't nullable and produce an error:

    #1048 - Column 'primakey' cannot be null 
    

    So after create temporary table I change the primary key to a be nullable.

    CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM table WHERE primarykey = 1;
    ALTER TABLE tmptable_1 MODIFY primarykey int(12) null;
    UPDATE tmptable_1 SET primarykey = NULL;
    INSERT INTO table SELECT * FROM tmptable_1;
    DROP TEMPORARY TABLE IF EXISTS tmptable_1;
    
    0 讨论(0)
  • 2020-11-27 10:08

    I just had to do this and this was my manual solution:

    1. In phpmyadmin, check the row you wish to copy
    2. At the bottom under query result operations click 'Export'
    3. On the next page check 'Save as file' then click 'Go'
    4. Open the exported file with a text editor, find the value of the primary field and change it to something unique.
    5. Back in phpmyadmin click on the 'Import' tab, locate the file to import .sql file under browse, click 'Go' and the duplicate row should be inserted.

    If you don't know what the PRIMARY field is, look back at your phpmyadmin page, click on the 'Structure' tab and at the bottom of the page under 'Indexes' it will show you which 'Field' has a 'Keyname' value 'PRIMARY'.

    Kind of a long way around, but if you don't want to deal with markup and just need to duplicate a single row there you go.

    0 讨论(0)
  • 2020-11-27 10:09

    This can be achieved with some creativity:

    SET @sql = CONCAT('INSERT INTO <table> SELECT null, 
        ', (SELECT GROUP_CONCAT(COLUMN_NAME) 
        FROM information_schema.columns 
        WHERE table_schema = '<database>' 
        AND table_name = '<table>' 
        AND column_name NOT IN ('id')), ' 
    from <table> WHERE id = <id>');  
    
    PREPARE stmt1 FROM @sql;
    EXECUTE stmt1;
    

    This will result in the new row getting an auto incremented id instead of the id from the selected row.

    0 讨论(0)
提交回复
热议问题