欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL实现LeetCode(182.重复的邮箱)

程序员文章站 2022-06-24 19:56:23
[leetcode] 182.duplicate emails 重复的邮箱write a sql query to find all duplicate emails in a table named...

[leetcode] 182.duplicate emails 重复的邮箱

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;

参考资料:

到此这篇关于sql实现leetcode(182.重复的邮箱)的文章就介绍到这了,更多相关sql实现重复的邮箱内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!