I have two databases that have contacts table in each. I want to find out what contacts exists in the primary db from the redundant one. primary database is sugarcrm. the one i want to make redundant is hdb. I have the following query which is not working

```
SELECT c.id, c.first_name, c.last_name
from `sugarcrm`.`contacts` c
left join `hdb`.`contacts` as cl
    ON cl.lastname = c.last_name
left join `hdb`.`contacts` as cf
    on cf.firstname = c.first_name

limit 0, 10000

```

I have a total of 10187 records in sugacrm.contacts and total of 8619 in hdb.contatcs.

I have tried this

```
SELECT c.id, c.first_name, c.last_name
from `sugarcrm`.`contacts` c
where not exists
(select * from `hdb`.`contacts`
where firstname != first_name and lastname != last_name
)
limit 0, 10000

```

returns 14 records

I want to include comparison with email address but my query is not working. To link email address in sugarcrm is as follows

email\_address table has id
email\_addr\_bean\_rel has email id and contact id

```
SELECT 
    c.id, 
    c.first_name, 
    c.last_name
FROM `sugarcrm`.`contacts` c, `sugarcrm`.`email_addresses` e, `sugarcrm`.`email_addr_bean_rel` er
LEFT JOIN `hdb`.`contacts` as cl
    ON cl.lastname = c.last_name
    AND cl.firstname = c.first_name
    AND cl.email = e.email_address
inner join `sugarcrm`.`contacts` 
    on er.bean_id = `sugarcrm`.`contacts`.id
inner join `sugarcrm`.`email_addr_bean_rel`
    on e.id = er.email_address_id

limit 0,100000

```

I have decided to use email, Managed to get the following using navicat

```
    SELECT
sugarcrm.email_addresses.email_address
FROM
sugarcrm.contacts
INNER JOIN sugarcrm.email_addr_bean_rel ON sugarcrm.contacts.id = sugarcrm.email_addr_bean_rel.bean_id
INNER JOIN sugarcrm.email_addresses ON sugarcrm.email_addresses.id = sugarcrm.email_addr_bean_rel.email_address_id
INNER JOIN hdb.contacts ON sugarcrm.email_addresses.email_address = hdb.contacts.EMAIL
LIMIT 0, 100000

```