MySQL查找并显示所有重复项

时间:2015-01-15 18:21:31

标签: mysql

我希望在数据库中找到重复项 - 但也会显示所有重复记录。我目前的查询:

SELECT 'duplicate' as dup,c.Id, c.CreatedDate, c.email, c.Lead_Grade__c, count(c.email) 
FROM contact as c
Inner Join (select c.Id, c.email FROM contact as c group by c.email having count(c.email) > 1) as dup
ON c.email = dup.email
WHERE Lead_Grade__c is null;

这可以为每个重复记录提供1行。我希望每个重复记录有2行(或更多行)。因此,如果记录X和记录Y都有相同的电子邮件,那么我想同时显示两者。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这会将联系人加入到自身,只显示电子邮件相同但有多条记录的记录(假设ID是唯一值)

此结果集只应重复。

SELECT A.* 
FROM contact A
INNER JOIN CONTACT B
 on A.email = B.email
 and a.id <> b.ID
ORDER BY A.Email