I am trying to insert new rows into table a and partially load the rows with data from table b. I would only like to insert new rows where the field b.id is not present in the a.id column.
Here are my tables:
table_a table_b
--------- -----------
id id
first_name first_name
country middle_name
last_name last_name
Here is my code:
INSERT INTO table_a a
SELECT b.id, b.first_name,b. last_name
FROM table_b b WHERE b.id <> a.id
The Question: How can I do this using an insert select statement? Please note, I’ve included middle name in one table and Country in another table to make this more comparable to my current situation. Is there a way to “map” the fields? or must I ensure that my insert select statement has the exact same number of values to insert as the target table? Or will it just look for matching column names and only update those columns that match?
an alternative solution would be using
LEFT JOINandIS NULL