182. 查找重复的电子邮箱(MySQL)(如何查找重复数据)

1 题目

在这里插入图片描述

2 MySQL

2.1 方法一(groupby……having)

不能把count判断放在where中
因为执行顺序是 from、where、groupby、having、select
count 必须在 groupby 之后执行

select Email
from Person
group by Email
having count(Email) > 1;

2.2 方法二(子表)

select Email
from(
    select Email, count(Email) as num
    from Person
    group by Email
) as subTable
where num > 1;

3 如何查找重复数据

3.1 方法一:groupby + having count()

select 列名
from 表名
group by 列名
having count(列名) > n;

3.2 方法二:from辅助表 + where

select 姓名 from
(
 select 姓名, count(姓名) as 计数
 from 学生表
 group by 姓名
) as 辅助表
where 计数 > 1;
发布了131 篇原创文章 · 获赞 0 · 访问量 2253

猜你喜欢

转载自blog.csdn.net/weixin_43969686/article/details/105704196