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 ;

相关推荐
Da Da 泓2 小时前
LinkedList模拟实现
java·开发语言·数据结构·学习·算法
海琴烟Sunshine2 小时前
Leetcode 14. 最长公共前缀
java·服务器·leetcode
未知陨落2 小时前
LeetCode:68.寻找两个正序数组的中位数
算法·leetcode
努力学习的小廉4 小时前
我爱学算法之—— 模拟(下)
c++·算法
麻雀20255 小时前
一键面试prompt
面试·职场和发展·prompt
海琴烟Sunshine5 小时前
Leetcode 26. 删除有序数组中的重复项
java·算法·leetcode
PAK向日葵6 小时前
【算法导论】NMWQ 0913笔试题
算法·面试
PAK向日葵6 小时前
【算法导论】DJ 0830笔试题题解
算法·面试
PAK向日葵6 小时前
【算法导论】LXHY 0830 笔试题题解
算法·面试
big\hero6 小时前
蓝桥杯13届省题
职场和发展·蓝桥杯