查找重复的电子邮件地址

时间:2013-03-05 12:56:00

标签: sql

我需要在我们的数据库中找到重复的电子邮件。我正在一张桌子上查看这些信息。到目前为止我有什么

 SELECT name.email, name.ID

 From Name
 Group BY Name.ID, Name.EMAIL
 Having Count(*) > 1

我知道这是错误的,但不确定如何恰当地写它。

5 个答案:

答案 0 :(得分:7)

删除ID

 SELECT name.email
 From   Name
 Group  BY Name.EMAIL
 Having Count(*) > 1

如果你想获得电子邮件的数量,

 SELECT name.email, COUNT(*) totalEmailCount
 From   Name
 Group  BY Name.EMAIL
 Having Count(*) > 1

答案 1 :(得分:2)

查询将是

SELECT name.email, COUNT(*) FROM Name 
GROUP BY Name.email HAVING COUNT(*) > 1

您需要知道的是,如果您按ID分组,则计数将为1,这就是您的查询无效的原因。

如果您需要知道电子邮件重复的用户的ID,您可以这样做:

select Name.ID, Name.Email from Name where Name.Email in (

SELECT name.email FROM Name 
    GROUP BY Name.email HAVING COUNT(*) > 1
)

答案 2 :(得分:2)

下面的SQL查询将返回包含相同(重复)EMAIL的第一条匹配行的ID和EMAIL

select ID, EMAIL from Name group by EMAIL having count(EMAIL) > 1

如果有人要从“名称”表中获取所有重复的EMAIL,则他/她可以执行以下SQL查询

select EMAIL from Name group by EMAIL having count(EMAIL) > 1

请注意:SQL是一种完全不区分大小写的语言。

答案 3 :(得分:1)

你走了:

SELECT name.email, COUNT(*)
FROM
    Name
GROUP BY
    Name.email
HAVING 
    COUNT(*) > 1

答案 4 :(得分:0)

 select id,email from 
 (select id,email,count(email) over (partion by email order by id) cnt from name) where cnt>1