6

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

 SELECT name.email, name.ID

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

我知道这是错误的,但不确定如何正确编写。

4

7 回答 7

12

去除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
于 2013-03-05T12:56:51.460 回答
4

查询将是

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
)
于 2013-03-05T13:10:11.603 回答
2

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

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

如果有人想要 Name 表中所有重复的 EMAIL,那么他/她可以执行以下 SQL 查询

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

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

于 2020-07-20T20:21:56.373 回答
1

干得好:

SELECT name.email, COUNT(*)
FROM
    Name
GROUP BY
    Name.email
HAVING 
    COUNT(*) > 1
于 2013-03-05T13:02:25.943 回答
0
 select id,email from 
 (select id,email,count(email) over (partion by email order by id) cnt from name) where cnt>1
于 2013-03-05T13:32:01.340 回答
0

About all posted answers here, using Group by methods. This query can also be written with self join method.

select distinct f.email
from Name f
join Name s
where f.id <> s.id and f.email = s.email
于 2021-10-09T09:26:36.080 回答
0

无需将 tableName 放在选择列中

SELECT email
From Name
Group BY EMAIL
Having Count(*) > 1
于 2022-01-08T05:32:29.323 回答