【LeetCode】182. Duplicate Emails

本文介绍两种SQL查询方法,用于从Person表中找出所有重复的电子邮件地址。第一种方法通过将表视为两个不同的表进行内部连接来查找具有相同电子邮件但不同ID的记录;第二种方法则使用group by和having子句来筛选出出现次数超过一次的电子邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Write a SQL query to find all duplicate emails in a table named Person.

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

For example, your query should return the following for the above table:

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

Note: All emails are in lowercase.

解决方法:

我是通过看成两个表(内交),内交的id有相同的邮箱说明重复使用了。

/*把一个表看成a,b两个表*/
select distinct(a.Email) from Person a,Person b where a.Id != b.Id and a.Email=b.Email;

别人的sq通过group by分组,要是有邮箱的数目大于1说明重复:
select email
from person
group by email
having count(email) >1;

这个执行的效率高,时间短。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值