查找所有重复的电子邮箱(如何查找一个字段中重复的数字)
题目
解析
思路一:子查询
先利用group by按相同的email分成一个组,做聚合统计,将每个email的数量计算出来,形成一个表,再在这个表里查询count(email)大于2的数量:
select Email,count(Email) as num from Person group by Email;
将上表作为被查询的表:
select Email from (select Email, count(Email) as num
from Person group by Email) as a where num >1;
思路二:group by 和 having
select Email from Person group by Email having count(Email)>1;