修改查询以显示所有电子邮件重复项

时间:2014-04-02 03:27:30

标签: mysql sugarcrm

有2个表

Table 1 : email_addresses 
Structure

id | email_address

1  | shhabji@gmail.com
2  | xyz@gmail.com
3  | abc@gmail.com

Table 2 :   email_addr_bean_rel
Structure
id | email_address_id|bean_module|bean_id|deleted

1  |   1  |  Contacts  | 1 |0
2  |   1  |  Contacts  | 1 |0
3  |   2  |  Contacts  | 2 |0

我希望查询通过email_address而不是特定的email_address显示所有重复项

目前我的查询是

SELECT ear.bean_id FROM email_addresses ea 
    LEFT JOIN email_addr_bean_rel ear ON ea.id = ear.email_address_id 
    WHERE ear.bean_module = 'Contacts' AND 
    ear.bean_id != 'ed87a55d-21fb-a459-276c-533a32df6f36' 
    AND ear.deleted = 0 AND ea.email_address LIKE '%shaabji@gmail.com%'

1 个答案:

答案 0 :(得分:0)

SELECT email_address, count(email_address) as 'Number_Of_Email_Address' FROM email_addresses
GROUP BY email_address HAVING count(email_address) > 1;

此查询将为您提供表1中的重复电子邮件地址。