insert into tablea select fields from tableb for specific values only

MYSQL No Comments »

While using insert into table select * from table there will be a problem that the total fields should match and the datatype should be same. But if there is table a and table b and you want to import table b’s few fields into table a then the above will not work and it will throw an error that total no of fields do not match.

So to import only specific fields then use the following sql in which only the needed fields are specified in the into part and the from part. so insert into table1 column names and select column names form table2.

INSERT INTO tablename (some_column, somecolumn2)
SELECT othercolumn1, othercolumn2 FROM TABLE name WHERE id=somevalue

the where clause is optional.

Notes to be taken

Entries RSS