题目
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
±—±--------+
| Id | Email |
±—±--------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
±—±--------+
根据以上输入,你的查询应返回以下结果:
±--------+
| Email |
±--------+
| a@b.com |
±--------+
思路
1.用group by 做分类,而having子句后跟聚合条件count(*) > 1
2. 用子查询语句
代码
法一
select email
from person
group by email
having count(*) > 1;
法2
select Email
from (select email,count(*) as sum
from person
group by email) as newtable
where sum >= 2;
收获
刚开始学习MySQL不久,主要在自己系统性地看书,希望在leetcode上多练练好准备面试!