What database are the tables in? Access, mySQL, MS SQL?
Here is some SQL that does everything you ask.
/*
check each table for a match from table 1. insert any match into table_5.
from what I can see, table 5 and 6 will be exact copies of each other, and we will make that last.
Some of this sql assumes that tracking_id is unique across all sites.
Table 5 and 6 have these fields:
name, address, county, zip, tracking_ID - from source table
Found_type - the way the record was matched. Name and addy or just addy
Found_Table - table the record was found in
*/
/* begin with table_1 to table_2. */
/* name and addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Name & Addy' as Found_type, 'Table_2' as Found_Table
from
table_1 inner join table_2 on table_1.name = table_2.name and table1.address = table_2.address
/* addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Addy' as Found_type, 'Table_2' as Found_Table
from
table_1 inner join table_2 on table_1.name = table_2.name and table1.address = table_2.address
WHERE NOT tracking_ID in (select tracking_ID from table_5) /* can also be done with an outer join if you want instead of a where clause*/
/* table_1 to table_3. */
/* name and addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Name & Addy' as Found_type, 'Table_3' as Found_Table
from
table_1 inner join table_3 on table_1.name = table_3.name and table1.address = table_3.address
WHERE NOT tracking_ID in (select tracking_ID from table_5) /* this will make it so records already found in table 2 are not entered again*/
/* addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Addy' as Found_type, 'Table_3' as Found_Table
from
table_1 inner join table_3 on table_1.name = table_3.name and table1.address = table_3.address
WHERE NOT tracking_ID in (select tracking_ID from table_5) /* this will make it so records already found in table 2 are not entered again*/
/* table_1 to table_4. */
/* name and addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Name & Addy' as Found_type, 'Table_4' as Found_Table
from
table_1 inner join table_4 on table_1.name = table_4.name and table1.address = table_4.address
WHERE NOT tracking_ID in (select tracking_ID from table_5) /* this will make it so records already found in table 2 & 3 are not entered again*/
/* addy match */
insert into table_5 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
select
name, address, county, zip, tracking_ID, 'Match on Addy' as Found_type, 'Table_4' as Found_Table
from
table_1 inner join table_4 on table_1.name = table_4.name and table1.address = table_4.address
WHERE NOT tracking_ID in (select tracking_ID from table_5) /* this will make it so records already found in table 2 & 3 are not entered again*/
/* table 5 now has all name and addy matches.
now we need to look for records from table 2,3,4 that were not found. The simpilist SQL statement would be to use the tracking_ID
and check tables 2,3,4 for an ID that does not exist.
*/
/* Compare all tracking ID's to those in table 5. any that are not found need to be put into table_7 */
/* table 2 */
insert into table_7 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
SELECT
name, address, county, zip, tracking_ID, 'Not in table_5' as Found_type, 'Table_2' as Found_Table
FRom
table_2
WHERE NOT tracking_ID IN (SELECT tracking_ID FROM table_5) /* any record not found should be inserted */
/* table 3 */
insert into table_7 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
SELECT
name, address, county, zip, tracking_ID, 'Not in table_5' as Found_type, 'Table_3' as Found_Table
FRom
table_3
WHERE NOT tracking_ID IN (SELECT tracking_ID FROM table_5) /* any record not found should be inserted */
/* table 4 */
insert into table_7 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
SELECT
name, address, county, zip, tracking_ID, 'Not in table_1' as Found_type, 'Table_4' as Found_Table
FRom
table_4
WHERE NOT tracking_ID IN (SELECT tracking_ID FROM table_5) /* any record not found should be inserted */
/* create a duplicate of table_5 for table_6 */
insert into table_6 (name, address, county, zip, tracking_ID, Found_type, Found_Table)
SELECT
name, address, county, zip, tracking_ID, Found_type, Found_Table
FRom
table_5