I have two tables:
people
| peopleID | Lastname | Firstname |
| -------- | -------- | --------- |
| 1 | Smith | Marc |
| 2 | Doe | John |
| 3 | Davidson | Terry |
| 4 | Meyer | Todd |
| 5 | Richards | Abe |
customers
| customerID | Lastname | Company |
| ---------- | -------- | ------------------- |
| 1 | Davidson | Wonderproducts Inc. |
| 2 | Meyer | Banana Inc. |
Now I want to insert all elements of the table people
to the table customers
, except the ones, where the lastname equals the lastname in customers
.
So at the end customers
should look like this:
| customerID | Lastname | Company |
| ---------- | -------- | ------------------- |
| 1 | Davidson | Wonderproducts Inc. |
| 2 | Meyer | Banana Inc. |
| 3 | Smith | |
| 4 | Doe | |
| 5 | Richards | |
I already tried around this:
IF NOT EXISTS
(SELECT 1 FROM customers WHERE Lastname = (SELECT Lastname FROM people))
INSERT INTO customers (Lastname) VALUES (SELECT Lastname FROM people)