编写一个 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. BigDecimal 的用法

    1.初始化 BigDecimal discount=new BigDecimal(0.9); BigDecimal discount=new BigDecimal(200); 2.加减乘除 加法 ad ...

  2. netcore中使用session

  3. Xshell设置密钥登录确保Linux

    用Xshell设置密匙登陆服务器, 第一步.使用Xshell生成密钥 我们打开熟悉的XSHELL软件,然后在工具-新建用户密钥生成向导. 到了生成密钥参数界面,我们这里需要选择RSA密钥类型,以及密钥 ...

  4. 网络拓扑_配置hybrid端口

    目的:实现不同VLAN间的PC不可互访, 但不同VLAN的PC均可以访问服务器. 例: VLAN5与VLAN10的PC不可以互通,但它们均可与服务器VLAN50互通. 拓扑图:

  5. mysql的mod函数

    取余是用函数mod(numer1,number2),其返回的值为其余数值 如:mod(id,2) = 1 返回id号是奇数的id

  6. C++中的const_cast

    开发环境 Qt Creator 4.8.2 编译器版本 MinGw 32-bit const_cast 用法: const_cast<type_id> (expression) 说明: 该 ...

  7. springboot+UEditor图片上传

    springboot+UEDitor百度编辑器整合图片上记录于此 1.下载ueditor插件包,解压到static/ueditor目录下 2.在你所需实现编辑器的页面引用三个JS文件 1)  uedi ...

  8. nginx-博客阅读笔记记录-20190916

    Nginx 入门学习教程 Ng官网解释: nginx [engine x]是最初由Igor Sysoev编写的HTTP和反向代理服务器,邮件代理服务器和通用TCP / UDP代理服务器. 维基百科解释 ...

  9. [python 学习] requests 库的使用

    1.get请求 # -*- coding: utf-8 -*- import requests URL_IP = "http://b.com/index.php" pyload = ...

  10. Docker之安装缺省指令

    Docker 中有些指令不存在,需要额外的安装,这里做下安装记录. 更新软件源中的所有软件列表 apt-get update 安装 ifconfig apt install net-tools 安装 ...