题目:
编写一个 SQL 查询,查找 Person
表中所有重复的电子邮箱。
示例:
解题思路:
方法一:
使用group by 按Email来分组,然后使用having选择count(id)> 1的就可以得到重复的Email。注意使用group by的时候,group by后面的字段一定要在select后面出现过。
MySQL语句:
1 # Write your MySQL query statement below 2 select Email 3 from Person 4 group by Email 5 having count(id) > 1;
方法二:
将 Email 分组后计算出相同 Email 的数量作为临时表,筛选出 num > 1 的邮箱
1 select t.Email 2 from (select Email, count(Email) as num from Person group by Email) as t 3 where t.num > 1;
小知识:
注意一下SQL语句结构中使用的先后顺序:
on > where > group by > having > order by >limit (顺口溜:哪里(where)组队(group by)有(having)顺序(order by)限制(limit))
标签:力扣,group,having,182,num,MySQL,Email,select From: https://www.cnblogs.com/liu-myu/p/16976932.html