[LeetCode] 182.查找重复的电子邮箱
编写一个 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 BY
和 HAVING
条件
向 GROUP BY
添加条件的一种更常用的方法是使用 HAVING
子句,该子句更为简单高效
。
所以我们可以将上面的解决方案重写为:
select Email
from Person
group by Email
having count(Email) > 1;
[LeetCode] 182.查找重复的电子邮箱的更多相关文章
- 182. 查找重复的电子邮箱 + group by + having
182. 查找重复的电子邮箱 LeetCode_MySql_182 题目描述 方法一:使用笛卡尔积 # Write your MySQL query statement below select di ...
- LeetCode:182.查找重复的电子邮箱
题目链接:https://leetcode-cn.com/problems/duplicate-emails/ 题目 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +- ...
- [SQL]LeetCode182. 查找重复的电子邮箱 | Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- MYSQL查询查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +----+---------+| Id | Email |+----+---------+| 1 | a@b.com | ...
- [LeetCode]196. 删除重复的电子邮箱(delete)
题目 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | ...
- [LeetCode] 196.删除重复的电子邮箱
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...
- LeetCode 182. Duplicate Emails (查找重复的电子邮箱)
题目标签: 题目给了我们一个 email 的table,让我们找到重复的 email. 可以建立 Person a, Person b, 找到两个表格中,emai 相等 但是 id 不同的 email ...
- LeetCode:196.删除重复的电子邮箱
题目链接:https://leetcode-cn.com/problems/delete-duplicate-emails/ 题目 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱 ...
- 【leetcode 简单】 第五十三题 删除重复的电子邮箱
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...
随机推荐
- Solr的学习使用之(五)添加索引数据
1.创建SolrServer类 SolrServer类:提供与Solr实例的连接与通信. 往Solr里添加索引数据,据说有好几种办法,这边利用SolrJ操作solr API完成index操作,具体So ...
- Linux 查找指定内容在哪个文件中
在实际的工作中,忘记配置项放在哪个文件中时,可借助命令来查询. eg: 1.grep -r "查询内容" 文件目录 #这样查询出来的包括文件名+内容 grep -r -l ...
- 八个JS中你见过的类型。
1.布尔类型 布尔值只能为 true 或者 false ,其他的会报错 let bool: boolean = false; bool = true; // bool = 123; // error ...
- AIX下绑定双网卡
摘要 AIX下绑定双网卡,实现IP地址的高可用.为后续按照oracle11gRAC环境做准备. 收 藏 生产环境中是将不同网卡的不同网口进行绑定.比如A网卡有A1,A2网口:B网卡有B1,B2网口 ...
- grunt教程
https://blog.csdn.net/sinat_38992528/article/details/79400595
- Jenkins slave-agent.jnlp运行无反应
在配置Jenkins的Windows节点时候,点击slave-agent.jnlp选择javaws.exe运行无反应,cmd命令执行javaws slave-agent.jnlp也不行,slave-a ...
- pycharm默认的模板修改python script
#!/usr/bin/env python # encoding: utf-8 #set( $SITE = "https://www.cnblogs.com/c-x-a" ) &q ...
- ProxyImpl 类
package com.test.mvp.mvpdemo.mvp.v7.proxy; import com.test.mvp.mvpdemo.mvp.v7.basemvp.BasePresenter; ...
- celery的入门使用
一.安装步骤 二.使用方法 三.和Django结合 四.部署和监控
- 测开之路六十一:接口测试平台之interface蓝图
create的js //添加header的函数function add_header() { // 这里是动态拼接html语句,带着样式,拼凑成页面的 "key [] value []&qu ...