问题描述:
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.
分析:这是典型的选择重复记录的数据库问题。这个问题还可以拓展一下,如果是获取多个字段同时相同(“与”)的记录,又该如何写呢?在解答中给出多个字段同时相同的记录的方法
解答:
本题解答:
select Email from Person group by Email having count(*)>1
选择多个字段同时相同(全部选择):
select * from tablename a where (a.col1,a.col2) in (select col1,col2 from table group by col1, col2 having count(*) > 1)