mysql--查找重复的电子邮件

浪尽此生 提交于 2019-11-30 18:36:05

 

 

解法一:(创建临时表当做子表来处理)

 

select Email from
(
  select Email, count(Email) as num
  from Person
  group by Email
) as statistic
where num > 1

 

  

解法二:(where好像只能用于原有数据表字段,聚合函数生成的字段无法配合使用)

 

select Email from Person group by Email having count(Email) > 1;

 

  

 

解法三:

select distinct a.Email from Person a, Person b where a.Email = b.Email and a.Id != b.Id 

 

  

 

 

 

 补充:

1、where后面不能跟聚合函数

2、group by 非聚合函数列  having 可以是聚合函数

 

3、where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。

 

4、having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!