Problem:
TableA contains a customer database
TableB contains new names that you want to match against your customer database and remove all the names that are already customers.
Solution:
Use “LEFT JOIN” property of mysql to carry out the task.
Lets assume that TableA and TableB have a field called “Phone” which contains the phone number of the clients and these numbers are obviously stored in a unique index. So what we are going to do is run a simple query to find out which numbers in TableB do not exist in TableA.
Assuming you are using linux and you can run this query from the command prompt, this is the way to do it:
user@server:~/ mysql -u USERNAME -p
(the server will ask for your password, enter the password and press Enter)
Now you should be in mysql and the prompt should look different already
mysql> use DATABASENAME
(this command changes the database to the one where you have both the tables)
mysql> select * from TableB LEFT JOIN TableA ON TableB.phone=TableA.phone WHERE TableA.phone IS NULL;
This should show you all the names in TableA that do not have a match in TableB.
Suppose you wanted to dump this file into a .txt file so you could use it elsewhere, the above command would change to:
mysql> SELECT * FROM TableB LEFT JOIN TableA ON TableB.phone=TableA.phone WHERE TableA.phone IS NULL INTO OUTFILE ‘/tmp/scrubbedfilename.txt’;
This will dump the file with the unmatching records from TableA in the /tmp directory of your linux filesystem.