1

我有两个数据库,每个数据库都有联系人表。我想从冗余数据库中找出主数据库中存在哪些联系人。主数据库是sugarcrm。我要冗余的是hdb。我有以下不工作的查询

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

我在 sugacrm.contacts 中共有 10187 条记录,在 hdb.contatcs 中共有 8619 条记录。

我试过这个

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

返回 14 条记录

我想包括与电子邮件地址的比较,但我的查询不起作用。在 sugarcrm 中链接电子邮件地址如下

email_address 表有 id email_addr_bean_rel 有 email id 和 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

我决定使用电子邮件,使用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
4

3 回答 3

1

更新:要检查主要而不是辅助数据库中的联系人,请尝试以下操作:

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
    AND cl.firstname = c.first_name
WHERE
    cl.lastname IS NULL

但:

  • 你应该使用一个 id (如果你有)加入不是 2 个字符串列

UPDATE2:回答“我将如何更改电子邮件查询以显示来自 hdb 且在 Sugarcrm 中不存在的联系人?”

SELECT
    hdb.*
FROM
    hdb.contacts
LEFT JOIN sugarcrm.email_addresses 
    ON sugarcrm.email_addresses.email_address = hdb.contacts.EMAIL
LEFT JOIN sugarcrm.email_addr_bean_rel 
    ON sugarcrm.email_addr_bean_rel.bean_id = sugarcrm.email_addresses.id
LEFT JOIN sugarcrm.contacts
    ON sugarcrm.contacts.id = sugarcrm.email_addr_bean_rel.bean_id
WHERE
    sugarcrm.contacts.id IS NULL
LIMIT 0, 100000
于 2013-05-31T09:39:04.883 回答
0

你可以试试这个

SELECT 
     c.id, c.first_name, c.last_name
from 
    `sugarcrm`.`contacts` c
where
     c.id in (select c1.id from `hdb`.`contacts` c1)

这给出了sugarcrm数据库中存在的记录

于 2013-05-31T09:56:50.950 回答
0

要在 hdb 中查找 Sugarcm 中不存在的用户,请尝试:

SELECT ch.id, ch.first_name, ch.last_name
from `hdb`.`contacts` ch
left join `sugarcrm`.`contacts` as cs
       ON ch.lastname = cs.last_name and cl.firstname = cs.first_name
where cs.first_name is null
于 2013-05-31T09:57:34.853 回答