简体   繁体   English

MySQL的插入到...选择

[英]mysql INSERT INTO … SELECT

I want to select one row in a table and insert it into another table like this: 我想在一个表中选择一行并将其插入到另一个表中,如下所示:

INSERT INTO  positionenneu
select *
from positionenneu_0603 where belegnummer like '4106192%'

I got an error because of duplicating id values. 我由于复制ID值而出错。 In both tables is id primary key AUTO_INCREMENT. 在两个表中,id都是主键AUTO_INCREMENT。 How can I do this without id column? 没有id列怎么办? Thank you for the answer. 谢谢你的回答。 Have a nice day! 祝你今天愉快!

Don't use the * and specify the column list instead, naturally omitting the id column: 不要使用*并指定列列表,而自然会省略id列:

INSERT INTO  positionenneu (foo, bar, baz)
select foo, bar, baz
from positionenneu_0603 where belegnummer like '4106192%'

Or you can of course use the id column somewhere if you need: 或者,您当然可以在需要的地方使用id列:

INSERT INTO  positionenneu (foo, bar, baz, positionenneu_0603_id)
select foo, bar, baz, id
from positionenneu_0603 where belegnummer like '4106192%'

It's good practice to specify the columns that you are inserting. 最好指定要插入的列。 This also allows you to skip the id field: 这还允许您跳过id字段:

INSERT INTO  positionenneu (field1,field2,field3)
    select field1_0603, field2_0603, field3_0603
    from positionenneu_0603 where belegnummer like '4106192%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM