编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例

+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email |
+---------+
| a@b.com |
+---------+

说明:所有电子邮箱都是小写字母。

题解:

方法一:使用 GROUP BY 和临时表

算法

重复的电子邮箱存在多次。要计算每封电子邮件的存在次数,我们可以使用以下代码。

select Email, count(Email) as num
from Person
group by Email;
| Email   | num |
|---------|-----|
| a@b.com | 2 |
| c@d.com | 1 |

以此作为临时表,我们可以得到下面的解决方案。

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

方法二:使用 GROUP BYHAVING 条件

GROUP BY 添加条件的一种更常用的方法是使用 HAVING 子句,该子句更为简单高效

所以我们可以将上面的解决方案重写为:

select Email
from Person
group by Email
having count(Email) > 1;

摘自:

https://leetcode-cn.com/problems/duplicate-emails/solution/cha-zhao-zhong-fu-de-dian-zi-you-xiang-by-leetcode/

[LeetCode] 182.查找重复的电子邮箱的更多相关文章

  1. 182. 查找重复的电子邮箱 + group by + having

    182. 查找重复的电子邮箱 LeetCode_MySql_182 题目描述 方法一:使用笛卡尔积 # Write your MySQL query statement below select di ...

  2. LeetCode:182.查找重复的电子邮箱

    题目链接:https://leetcode-cn.com/problems/duplicate-emails/ 题目 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +- ...

  3. [SQL]LeetCode182. 查找重复的电子邮箱 | Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  4. MYSQL查询查找重复的电子邮箱

    编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +----+---------+| Id | Email |+----+---------+| 1 | a@b.com | ...

  5. [LeetCode]196. 删除重复的电子邮箱(delete)

    题目 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | ...

  6. [LeetCode] 196.删除重复的电子邮箱

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...

  7. LeetCode 182. Duplicate Emails (查找重复的电子邮箱)

    题目标签: 题目给了我们一个 email 的table,让我们找到重复的 email. 可以建立 Person a, Person b, 找到两个表格中,emai 相等 但是 id 不同的 email ...

  8. LeetCode:196.删除重复的电子邮箱

    题目链接:https://leetcode-cn.com/problems/delete-duplicate-emails/ 题目 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱 ...

  9. 【leetcode 简单】 第五十三题 删除重复的电子邮箱

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...

随机推荐

  1. 如何在springboot中读取自己创建的.properties配置的值

    在实体类里面加上 @PropertySource("classpath:/robot_config.properties") robot_config.properties // ...

  2. liunx-centos-基础命令详解(1) -主要内容来自 —https://www.cnblogs.com/caozy/p/9261224.html

    关机:halt/poweroff :立刻关机reboot :立刻重启 shutdown -r now :立刻重启shutdown -h 00:00 :定时重启 now:立刻shutdown -h +n ...

  3. 21eval 函数

    eval() 函数十分强大 ---- 将字符串 当成 有效的表达式 来求职 并 返回计算结果 # 基本的数学计算 # 字符串重复 print(eval("'*' * 5")) # ...

  4. $PMTargetFileDir 参数位置

    系统/session参数与变量参数和变量都配置在Session中,如$PMTargetFileDir.$PMBadFileDir等.这些变量有哪些.在哪里定义.是否可以修改呢?在控制台(Admin C ...

  5. [Luogu2015]二叉苹果树(树形dp)

    [Luogu2015] 二叉苹果树 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. ...

  6. dialog写进dll调用

    #ifdef DLG_WINDOW_API #define DLG_WINDOW_EXPORT __declspec(dllexport) #else #define DLG_WINDOW_EXPOR ...

  7. oracle 汇编04

    General-Purpose Instructions The general-purpose instructions perform basic data movement, memory ad ...

  8. 切面AOP的切点@Pointcut用法

    格式: execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern ...

  9. thinkphp url和路由

    一.入口模块修改 修改public下的index 加入 define('BIND_MODULE','admin'); 即可将入门模块绑定到admin模块 <?php // [ 应用入口文件 ] ...

  10. js如何判断用户使用的设备类型及平台

    前端开发经常遇到需要判断用户的浏览设备,是pc端还是移动端,移动端使用的是什么手机系统?android.ios.ipad.windows phone等等,有时候还需要知道用户浏览页面是在微信中打开还是 ...