I have two tables in Access database. Table1 has more columns than Table2. I would like to merge those tables into one while removing duplicates. I have the following query
SELECT FirstName, LastName, PhoneNumber FROM Table1
UNION
SELECT FirstName, LastName, Null as PhoneNumber FROM Table2
Problem is, I don't want to copy any entry from Table2 that has the same FirstName and LastName in Table1. How can I change the above query to accomplish that? Thanks in advance.
FaddishWorm has a good concept, but the two separate subqueries would eliminate any record with matching firstname OR matching lastname. NOT x AND NOT y = NOT(x OR y). So names like Hernandez and Jim would be omitted from TABLE2.
Try concatenating.
There are other solutions. This is slow. HandsUp has the right idea.
Start with a query which returns only those
Table2
rows which are not matched inTable1
.Then use that
SELECT
in yourUNION
query.Note I used
UNION ALL
because it requires less work by the db engine so is therefore faster. Use justUNION
when you want the db engine to weed out duplicate rows. But, in this case, that would not be necessary ... unless duplicates exist separately within one or both of those tables.Try constraining like follows: