1

I have two tables and I want to copy a row from one to the other. However they both use an auto incrementing ID number as the primary key which is preventing me from copying a row if the ID already exists in the other table.

What I want to do is:

INSERT INTO tableB SELECT * FROM tableA WHERE ID = 1

What I was thinking might be possible would be to store the row as a variable, temporarily set the ID to be blank, and then it will be assigned the next highest number once inserted into the new table?

Is something like this possible?

Edit:

Columns

ID, Deal_ID, Redeemed_At, Wowcher_Code, Deal_Title, Customer_Name, House_Name_Number, Address_Line_1, Address_Line_2, City, County, Postcode, Email, Phone, Date_of_Birth, Custom_Field, Marketing_Permission, Product_Name, Product_Options

Edward1442
  • 143
  • 2
  • 2
  • 12

3 Answers3

1

You cannot use * (all columns) in this case .. you must explicitally set the columns you need eg:

INSERT INTO tableB (col1, col2, col3) 
SELECT col1, col2, col3 
FROM tableA 
WHERE ID = 1

avoinding the ID columns

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

Just select the columns without the ID.

INSERT INTO tableB SELECT name,age,any_other_column FROM tableA WHERE ID = 1
Rato Zumbi
  • 63
  • 1
  • 7
0

Try replace asterisks with fields list withouth ID

INSERT INTO tableB SELECT Field1,Field2,Field3...FieldXX FROM tableA WHERE ID = 1
Stilet
  • 92
  • 2