leetcode-mysql

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

表: Person

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

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

任意顺序返回结果表。

结果格式如下例。

示例 1:

复制代码
输入: 
Person 表:
+----+---------+
| id | email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+
输出: 
+---------+
| Email   |
+---------+
| [email protected] |
+---------+
解释: [email protected] 出现了两次。

首先:

可以看到,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 ;

相关推荐
GGBondlctrl19 分钟前
【leetcode】《BFS扫荡术:如何用广度优搜索征服岛屿问题》
算法·leetcode·bfs·宽度优先·图像渲染·岛屿的数量·被围绕的区域
星沁城1 小时前
236. 二叉树的最近公共祖先
java·数据结构·leetcode·二叉树
容辞4 小时前
算法-贪婪算法
算法·贪心算法
Evand J4 小时前
MATLAB程序演示与编程思路,相对导航,四个小车的形式,使用集中式扩展卡尔曼滤波(fullyCN-EKF)
人工智能·算法
椰萝Yerosius6 小时前
[题解]2023CCPC黑龙江省赛 - Ethernet
算法·深度优先
IT猿手7 小时前
基于 Q-learning 的城市场景无人机三维路径规划算法研究,可以自定义地图,提供完整MATLAB代码
深度学习·算法·matlab·无人机·强化学习·qlearning·无人机路径规划
竹下为生9 小时前
LeetCode --- 448 周赛
算法·leetcode·职场和发展
未名编程9 小时前
LeetCode 88. 合并两个有序数组 | Python 最简写法 + 实战注释
python·算法·leetcode
Cuit小唐9 小时前
C++ 迭代器模式详解
c++·算法·迭代器模式
2401_858286119 小时前
CD37.【C++ Dev】string类的模拟实现(上)
开发语言·c++·算法