0

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. In both tables is id primary key AUTO_INCREMENT. How can I do this without id column? Thank you for the answer. Have a nice day!

2
  • do you know difference between Insert and Select? Commented Mar 6, 2014 at 15:14
  • don't select *, then. select all,the,fields,except,the,ones,you,dont,want ... Commented Mar 6, 2014 at 15:21

2 Answers 2

1

Don't use the * and specify the column list instead, naturally omitting the id column:

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:

INSERT INTO  positionenneu (foo, bar, baz, positionenneu_0603_id)
select foo, bar, baz, id
from positionenneu_0603 where belegnummer like '4106192%'
Sign up to request clarification or add additional context in comments.

Comments

0

It's good practice to specify the columns that you are inserting. This also allows you to skip the id field:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.