美文网首页
LeetCode 182. Duplicate Emails

LeetCode 182. Duplicate Emails

作者: 扑哧咳哧 | 来源:发表于2018-07-12 16:49 被阅读0次
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.

分析:
1.用count函数结合group by可以统计指定字段的数量。假如重复的话,count函数肯定大于1.
2.当进过第一步后,再从第一步后的临时表中筛选count >1的字段记录.
结果:

# Write your MySQL query statement below
SELECT Email 
from(
        SELECT Email, COUNT(Email) as count
        FROM Person GROUP BY Email
) t 
where t.count > 1;

相关文章

网友评论

      本文标题:LeetCode 182. Duplicate Emails

      本文链接:https://www.haomeiwen.com/subject/wrerpftx.html