Mysql训练:where后不可以进行聚合函数的判断,而having可以进行聚合函数的判断

力扣题目:查找重复的电子邮箱

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

说明:所有电子邮箱都是小写字母。

我写的SQL:

select Email from (select count(Email) as c,Email from Person group by Email) T where T.c>1;

由于having可以进行聚合函数的判断,SQL可以更简洁

select Email from Person group by Email having count(Email)>1;
原文地址:https://www.cnblogs.com/zwh0910/p/14395376.html