mySql copy rows into same table with key value changed (not overwriting existing)

Viewed 30513

How do I copy a selection of rows from a mySql table and insert with just the key value changed. Can I do a select and insert in same query?

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

Table cols, 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 the insert 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                   |
+----+------+---------------------------+
3 Answers

you can do

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