182-查找重复的电子邮箱
题目描述
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
|----|---------|
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
说明:所有电子邮箱都是小写字母。
题目链接链接:https://leetcode-cn.com/problems/duplicate-emails
# Write your MySQL query statement below
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 前者使用针对 Eamil 分组的方法,再进行判断有多少个重复 Email
2 后者使用表的连接,然后再判断重复的,但是结果需要去重
性能分析
可以使用两种方法进行查询,前者更快一点,表的大小对这两个查询的性能有很大的影响。