Reputation: 1229
I have two tables, TableA and TableB
TableA has 9 fields TableB has 7 fields
There are 2 fields (id and name) that are identical in both tables, is there a way to select ONLY these two fields from TableA and insert them into TableB?
I have looked at the INSERT INTO... SELECT method using this statement:
INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1
But I get the following error:
#1136 - Column count doesn't match value count at row 1
I assume this error is not allowing me to insert only 2 fields into the table? If so, is there a way around this or an alternative method?
Thanks
Upvotes: 3
Views: 1309
Reputation: 5101
Specify the columns in table b
INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1
Upvotes: 1
Reputation: 531
You need to specify the column names for TableB (and possibly specify TableA.id in the WHERE clause):
INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1
Upvotes: 2
Reputation: 3038
Try:
INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;
One would have to assume that the column names in TableB match TableA otherwise you would need to put in the right names.
Upvotes: 6