原题:https://leetcode-cn.com/problems/duplicate-emails/
我自己做leetcode的数据库题目的解题记录:
解题目录 https://blog.csdn.net/weixin_42845682/article/details/105196004
题目描述:
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
答案:
第一种答案
感觉这道题很简单,没啥做的。。。
select
distinct(p1.Email) email
from Person p1
join Person p2 on p1.email = p2.email and p1.id != p2.id
第二种答案
我感觉,这道题应该有好几种解决办法,而且第一种办法看的好蠢。。。
select
Email
from
(
select
Email,
count(Email) c
from Person
group by Email
) tmp
where c >1
第三种答案
第二种答案看着好麻烦…其实用having省略一下就行。
select
Email
from Person
group by Email
having count(Email) >1