Sugar CRM - MYSQL 显示具有相同电子邮件地址的联系人

Sugar CRM - MYSQL show contacts with same email address

如果我在 Sugar CRM 中搜索,我可以找到一些已分配给多个联系人的电子邮件地址 - 我不想要这个。我希望有一个 SQL 查询,它将列出与多个联系人关联的所有电子邮件地址。所以换句话说,列出所有具有相同电子邮件地址的 Sugar 联系人。

我尝试了各种查询,但都卡住了。下面的查询将允许我找到一堆联系人,其中

SELECT ea.email_address, eabr.* FROM email_addr_bean_rel eabr
INNER JOIN email_addresses ea ON eabr.email_address_id = ea.id
WHERE ea.email_address = 'joe.bloggs@gmail.com'
AND eabr.deleted = 0;

没关系,但我必须指定要查看的电子邮件帐户。

任何人都可以帮我重写这个,以便不指定电子邮件地址,但搜索整个数据库以查找存在多个联系人的任何电子邮件地址吗?

我不确定如何编写上面的查询。

如有指点,将不胜感激。

谢谢

詹姆斯

首先,您 join 表格。接下来,过滤掉已删除的元素。然后,您按 ea.email_address 分组。因此,您检查它们的数量是否大于 1。最后,您 select 分组 ea.email_address 并使用分隔符合并 contact_id 值(在本例中为 ':')使用 group_contact.

SELECT ea.email_address, GROUP_CONCAT(eabr.contact_id SEPARATOR ':') FROM email_addr_bean_rel eabr
INNER JOIN email_addresses ea ON eabr.email_address_id = ea.id
where eabr.deleted = 0
group by ea.email_address
having count(*) > 1

代码未经测试。

如果您想要任何重复的电子邮件地址,您可以尝试这样的操作:

SELECT ea.email_address, eabr.* FROM email_addr_bean_rel eabr
INNER JOIN email_addresses ea ON eabr.email_address_id = ea.id
WHERE ea.email_address IN
(SELECT ea.email_address FROM email_addr_bean_rel eabr
INNER JOIN email_addresses ea ON eabr.email_address_id = ea.id
WHERE  eabr.deleted = 0
GROUP BY email_address
HAVING count(*) > 1)
AND eabr.deleted = 0;