Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to SELECT fields from one table and INSERT to another?

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

like image 750
Bernard Avatar asked Feb 01 '26 22:02

Bernard


1 Answers

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.

like image 81
Karlson Avatar answered Feb 04 '26 15:02

Karlson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!