MySql copies rows to the same table with the changed key value (without overwriting the existing ones)

How to copy the selected row from the mySql table and paste only with the changed key value. Can I select and paste into the same query?

To be precise, what I want would look like this:

Columns, ID and ISO3 are keys:

+----+------+-------------------------+ | ID | ISO3 | Text | +----+------+-------------------------+ | 1 | ENU | A text in english | | 2 | ENU | Another text in english | | 3 | ENU | bla bla | | 1 | JPN | δΈŽγˆγ‚‰γ‚ŒγŸζž  | +----+------+-------------------------+ 

After pasting, I want my table to look like this:

 +----+------+---------------------------+ | ID | ISO3 | Text | +----+------+---------------------------+ | 1 | ENU | A text in english | | 2 | ENU | Another text in english | | 3 | ENU | bla bla | | 1 | JPN | δΈŽγˆγ‚‰γ‚ŒγŸζž  | | 2 | JPN | Another text in english | | 3 | JPN | bla bla | +----+------+---------------------------+ 
+10
source share
3 answers
 INSERT INTO your_table (ID, ISO3, TEXT) SELECT ID, 'JPN', TEXT FROM your_table WHERE ID IN ( list_of_ id ) 

If you want to change the value in one cell, just enter the value hard, and do not select from the table (as I did with 'JPN ').

+26
source

If your key is auto_increment and you have a table structure, for example primary_key | col1 | col2 | col3 primary_key | col1 | col2 | col3 primary_key | col1 | col2 | col3 , you can do something in the lines

 INSERT INTO table ( col1, col2, col3 ) SELECT col1, col2, col3 FROM table WHERE primary_key IN(1, 2, 45, 54, 774, 4434 /* etc */); 
+5
source

You can do

 insert into your_table (SELECT field1, field2,(changed_value_for_field3) as field3 FROM your_table where your_condition); 
0
source

All Articles