Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| [email protected] |
+---------+
Note: All emails are in lowercase.
題意:查找表中重復的Email.
此題是很典型的對分組結果進行統計篩選例題,因此可以利用group by進行分組,然后使用having統計.
# Write your MySQL query statement below
SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1;
此處,對where與group by進行比較(參考自:https://leetcode-cn.com/problems/duplicate-emails/solution/cha-zhao-zhong-fu-de-dian-zi-you-xiang-by-he-qing-/):
where后不能跟聚合函式,因為where執行順序大于聚合函式,where子句的作用是在對查詢結果進行分組前,將不符合where條件的行去掉,即在分組之前過濾資料,條件中不能包含聚組函式,使用where條件顯示特定的行,having子句的作用是篩選滿足條件的組,即在分組之后過濾資料,條件中經常包含聚組函式,使用having條件顯示特定的組,也可以使用多個分組標準進行分組,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/121214.html
標籤:MySQL
