mySql 将行复制到同一个表中,键值已更改(不覆盖现有)

2022-01-20 00:00:00 复制 mysql

如何从 mySql 表中复制选择的行并在仅更改键值的情况下插入.我可以在同一个查询中进行选择和插入吗?

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:

表格列、ID 和 ISO3 是键:

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                   |
+----+------+---------------------------+

推荐答案

INSERT INTO your_table (ID, ISO3, TEXT) 
SELECT ID, 'JPN', TEXT
FROM your_table 
WHERE ID IN ( list_of_ id's )

如果您想更改一个单元格中的值,只需硬输入值而不是从表格中选择(就像我使用 'JPN' 所做的那样).

If you want to change a value in one cell, just hard-type the value instead of selecting from table (like I did with 'JPN').

相关文章