問題:spa
Write a SQL query to find all duplicate emails in a table named Person
.code
+----+---------+ | 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:it
+---------+ | Email | +---------+ | a@b.com | +---------+
Note: All emails are in lowercase.table
解決:email
① 用Group by...Having Count(*)...的固定搭配來作。987 mstab
SELECT Email FROM Person GROUP BY Email
HAVING COUNT(*) > 1;vi
② 能夠用內交來作,用Email來內交兩個表,而後返回Id不一樣的行,則說明兩個不一樣的人使用了相同的郵箱。1037 msquery
SELECT DISTINCT p1.Email FROM Person p1
JOIN Person p2 ON p1.Email = p2.Email
WHERE p1.Id <> p2.Id;mail
SELECT DISTINCT p1.Email FROM Person p1,Person p2
WHERE p1.Email = p2.Email AND p1.Id <> p2.Id;co