leetcode-mysql

182. 查找重复的电子邮箱 - 力扣(LeetCode)

表: Person

复制代码
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id 是该表的主键(具有唯一值的列)。
此表的每一行都包含一封电子邮件。电子邮件不包含大写字母。

编写解决方案来报告所有重复的电子邮件。 请注意,可以保证电子邮件字段不为 NULL。

任意顺序返回结果表。

结果格式如下例。

示例 1:

复制代码
输入: 
Person 表:
+----+---------+
| id | email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+
输出: 
+---------+
| Email   |
+---------+
| a@b.com |
+---------+
解释: a@b.com 出现了两次。

首先:

可以看到,email 变为了Emai所以需要取别名

select email as Email

出现了两次,但是输出只输出了一次

select distinct email as Email

要的是重复的email

where p1.email = p2.email

and p1.id !!= p2.id

中和一下上面的

SELECT DISTINCT p1.email AS Email

FROM Person p1 ,Person p2

where

p1.email=p2.email

and

p1.id!=p2.id;

成功

看答案

答案的方法是

先找到email,然后算出email出现的次数

将此时的这个表作为临时的表,借此算出Email的表(本题需要的结果)

select Email from

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

Email

from

(

select Email, count(Email) as num

from Persons

group by Email)

as statistic

-- statistic为临时的表

where num > 1 ;

相关推荐
励志要当大牛的小白菜24 分钟前
ART配对软件使用
开发语言·c++·qt·算法
qq_5139704427 分钟前
力扣 hot100 Day56
算法·leetcode
PAK向日葵1 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱喝矿泉水的猛男3 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao4 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian7724 小时前
Traffic Lights set的使用
算法
go54631584656 小时前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
aramae6 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
大锦终6 小时前
【算法】前缀和经典例题
算法·leetcode
想变成树袋熊6 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理