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 ;

相关推荐
_日拱一卒25 分钟前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
jiayong232 小时前
AI架构师面试题库 - 完整汇总文档
人工智能·面试·职场和发展
用户987409238873 小时前
llamafactory 0.6.3 没有 llamafactory-cli
算法
计算机安禾3 小时前
【算法分析与设计】第26篇:参数化算法与固定参数可解性理论
大数据·人工智能·算法·机器学习·剪枝
AI科技星3 小时前
基于**v=c(空间光速螺旋运动)唯一第一性原理**重新完整求导证明
人工智能·线性代数·算法·机器学习·架构·概率论·学习方法
风筝在晴天搁浅4 小时前
美团 LeetCode 692.前K个高频单词
算法·leetcode·职场和发展
地平线开发者4 小时前
量化训练时 fusebn/withbn 简介
算法·自动驾驶
不做无法实现的梦~4 小时前
MAVLink 协议教程
linux·stm32·嵌入式硬件·算法
墨白曦煜4 小时前
算法实战笔记:剥开回溯算法的外衣——从通用模板到高阶去重(八)
笔记·算法
z200509305 小时前
今日算法(回溯子集)(模版题)
数据结构·算法·leetcode