[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实现重复的邮箱内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!