分组过滤查询【查找重复的电子邮箱】leetCode

SELECT email FROM Person
GROUP BY email having count(email)>1

分组过滤查询【查找重复的电子邮箱】leetCode_第1张图片

 分组过滤查询【查找重复的电子邮箱】leetCode_第2张图片

 分组过滤

select email from Person

group by email

having count(Email) >1;

数据库person表数据有10条,如下所示

分组过滤查询【查找重复的电子邮箱】leetCode_第3张图片

表中数据如图

分组过滤查询【查找重复的电子邮箱】leetCode_第4张图片


 分组过滤查询【查找重复的电子邮箱】leetCode_第5张图片

使用group by 和having,优先顺序:where>group by>having>order by

分组显示数量

分组过滤查询【查找重复的电子邮箱】leetCode_第6张图片

 group by分组是优先于count函数的

考虑到可能会有不重复的数据,没有相同email邮箱的,num为1,我们只要找到不重复的数据就可以了,所以还要过滤一遍

分组过滤查询【查找重复的电子邮箱】leetCode_第7张图片

 COUNT(DISTINCT expr,[expr...])

DISTINCT去重

  1. DISTINCT 表示对后面的所有参数的拼接取 不重复的记录,即查出的参数拼接每行记录都是唯一的

SELECT DISTINCT p1.email AS Email

FROM Person p1 JOIN Person p2 ON p1.email=p2.email

WHERE p1.id!=p2.id;

子查询

select email from
(
    select email,count(email) as num

    from Person

    group by email

)as temp

where num >1;

你可能感兴趣的:(leetcode,算法,职场和发展)