力扣数据库题目182查找重复的电子邮箱

力扣数据库题目182查找重复的电子邮箱mysql

题目

编写一个 SQL 查询,查找 Person 表中全部重复的电子邮箱。sql

示例:数据库

+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+code

根据以上输入,你的查询应返回如下结果:blog

+---------+
| Email |
+---------+
| a@b.com |
+---------+class

说明:全部电子邮箱都是小写字母。test

来源:力扣(LeetCode)email

方案一

分组im

SELECT email
FROM test.person
GROUP BY email
HAVING count(*) > 1

方案二

where子查询总结

SELECT DISTINCT email
FROM test.person t
WHERE (SELECT count(*) FROM test.person WHERE email = t.email) > 1

方案三

exists判断

SELECT DISTINCT email
FROM test.person t
WHERE EXISTS(SELECT email FROM test.person WHERE email = t.email AND id <> t.id)

方案四

表链接

SELECT DISTINCT a.email
FROM test.person a
INNER JOIN test.person b ON a.email = b.email AND a.id > b.id

总结

通常状况下查询能够先考虑简单查【标准单表查询】,再考虑子查询【SELECT或WHERE子查询】,最后考虑表链接。基本上表链接能够解决几乎全部SQL问题。

相关文章
相关标签/搜索