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 ;

相关推荐
不会就选b44 分钟前
算法日常・每日刷题--<贪心>14
算法
mmmmath_33 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z4 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
醇氧5 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
大熊背5 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温
钓鱼的肝5 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
参.商.5 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
HZZD_HZZD5 小时前
CSDN_批发市场水电漏损归因算法LAM的原理与落地
嵌入式硬件·物联网·算法
shirsl7 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法