[LeetCode] Duplicate Emails 重复的邮箱
2024-08-31 09:14:44
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.
这道题让我们求重复的邮箱,那么最直接的方法就是用Group by...Having Count(*)...的固定搭配来做,代码如下:
解法一:
SELECT Email FROM Person GROUP BY Email
HAVING COUNT(*) > 1;
我们还可以用内交来做,用Email来内交两个表,然后返回Id不同的行,则说明两个不同的人使用了相同的邮箱:
解法二:
SELECT DISTINCT p1.Email FROM Person p1
JOIN Person p2 ON p1.Email = p2.Email
WHERE p1.Id <> p2.Id;
参考资料:
https://leetcode.com/discuss/53206/a-solution-using-a-group-by-and-another-one-using-a-self-join
LeetCode All in One 题目讲解汇总(持续更新中...)
最新文章
- ASP.NET中cookie与Fiter实现简单登陆,AllowAnonymous匿名登陆
- junit
- CentOS配置163的yum源
- Text input(文本输入框)
- 女友要下岗?Windows 10的Cortana何方妖孽?
- C#出题库项目的总结(1)
- 【转载】Redis多实例及分区
- Java中ThreadLocal的深入理解
- JSON漫谈
- tomcat gc问题总结
- PHPMailer发匿名邮件及Extension missing: openssl的解决
- PHP安全编程:跨站脚本攻击的防御(转)
- Spring MVC(一)
- 基于vue2.0前端组件库element中 el-form表单 自定义验证填坑
- Spark MLlib KMeans 聚类算法
- RTMP、HTTP-FLV、HLS,你了解常见的三大直播协议吗
- Android多线程的使用
- Win10注册表无法保存对权限所作的更改拒绝访问
- ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]
- 织梦替换ueditor百度编辑器,支持图片水印 教程