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

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


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?

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

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,与えられた枠

表格列表,ID和ISO3是键:[ID] [ISO3] [文本] 1,ENU,英文文本2,ENU,英文3中的另一文本,ENU,bla bla 1,JPN,与えられた枠

After the insert I want my table to look like this:
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

插入后,我希望我的表格看起来像这样:1,ENU,英文文本2,ENU,英文3中另一文本,ENU,bla bla 1,JPN,与えられた枠2,JPN,另一个英文文本3,JPN,bla bla

3 个解决方案

#1


20  

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

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

如果你想改变一个单元格中的值,只需硬键入值而不是从表中选择(就像我用'JPN'那样)。

#2


4  

If your key is auto_increment, and you have a table structure like primary_key | col1 | col2 | col3 then you can do something along the lines of

如果您的密钥是auto_increment,并且您有一个类似primary_key的表结构col1 | col2 | col3然后你可以做一些事情

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

#3


0  

you can do

你可以做

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/07/01/c1b1a81ff7234605076a5882038a3afd.html



 
  © 2014-2022 ITdaan.com 联系我们: