1.编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
select Num as ConsecutiveNums  from Logs where Id in (
select distinct l1.Id from Logs l1,Logs l2,Logs l3
where (l1.Num = l2.Num and l2.Num = l3.Num and (
(l1.Id + = l2.Id and l2.Id + = l3.Id)or
(l3.Id + = l2.Id and l2.Id + = l1.Id)or
(l3.Id + = l1.Id and l1.Id + = l2.Id)
)
)
order by l1.Id) group by Num

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

示例:

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

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

+---------+
| Email |
+---------+
| a@b.com |
+---------+
select Email from Person group by Email having count(Email)>

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

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id 是这个表的主键
delete p1 from Person p1,Person p2 where p1.Email=p2.Email and p1.Id>p2.Id

mysql查询之 连续出现的数字,重复出现的邮箱,删除重复的电子邮箱的更多相关文章

  1. mysql——查询重复数据,及删除重复数据只保留一条数据

    查询 text 表中,user_name字段值重复的数据及重复次数 select user_name,count(*) as count from text 删除 text 表中,重复出现的数据只保留 ...

  2. Python 迭代删除重复项,集合删除重复项

    1. 迭代删除重复项:先排序列表项,然后通过新迭代(not in)去除重复项,分片打印 def sanitize(time_string): if '-' in time_string: splitt ...

  3. MySQL查询显示连续的结果

    #mysql中 对于查询结果只显示n条连续行的问题# 在领扣上碰到的一个题目:求满足条件的连续3行结果的显示 X city built a new stadium, each day many peo ...

  4. mysql查询中字符串转换成数字

    在操作mysql时,经常需要将字符转换成数字,这一步虽然简单,但不常用的话也很容易忘记,现将在网上找到的方法记录如下: 1.将字符的数字转成数字,比如'0'转成0可以直接用加法来实现例如:将pony表 ...

  5. 删除表中重复数据,只删除重复数据中ID最小的

    delete t_xxx_user where recid in ( select recid from t_xxx_user where recid in ( select min(recid) f ...

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

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

  7. mysql 删除重复数据

    1在日常使用mysql中 前端页面点击次数过多 mysql就会容易产生冗余数据,那这些数据该怎么删除呢 说下思路 查询重复字段id 查询重复字段最小id 删除重复字段ID 保留最小ID 查询重复记录这 ...

  8. mysql删除重复数据,保留最新的那一条

    因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如 ...

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

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

随机推荐

  1. 数组,字符串,json互相转换

    数组转字符串 var arr = [1,2,3,4,'巴德','merge']; var str = arr.join(','); console.log(str); // 1,2,3,4,巴德,me ...

  2. python通过LXML库读取xml命名空间

    xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...

  3. react编写规范之组件组件的内容编写顺序

    static 开头的类属性,如 defaultProps.propTypes. 构造函数,constructor. getter/setter(还不了解的同学可以暂时忽略). 组件生命周期. _ 开头 ...

  4. 洛谷 P3398 仓鼠找sugar 题解

    每日一题 day44 打卡 Analysis 首先有一个结论:先找 p1=(a,b),p2=(c,d) 的LCA的深度,在与(a,c),(a,d),(b,c),(b,d)中最深的LCA n的深度比较, ...

  5. win7虚拟机安装

    https://blog.csdn.net/qq_16503045/article/details/81904986 iso下载地址 https://msdn.itellyou.cn/

  6. requestLayout() improperly called by xxxxxxxxxxxxxxxxxxx ScrollViewContainer 问题

    当scrollview内的内容更改大小时,Scrollview不会自行调整大小.效果是,当内容变小时,内容将留在原来的位置,当内容变大时,无法看到.仅当ScrollView位于作为MasterDeta ...

  7. REdis主从复制之repl_backlog

    目录 目录 1 1. 前言 1 2. 配置项 1 3. redisServer 2 4. feedReplicationBacklog-写repl_backlog 3 5. addReplyRepli ...

  8. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  9. 小数据池/is和==/再谈编码作业

    # 1,老男孩好声选秀大赛评委在打分的时候呢, 可以输入分数. 假设, 老男孩有10个评委. 让10个评委进行打分, 要求, 分数必须高于5分, 低于10分.将每个评委的打分情况保存在列表中. pin ...

  10. GoCN每日新闻(2019-10-06)

    GoCN每日新闻(2019-10-06) 国庆专辑:GopherChina祝大家国庆节快乐 GoCN每日新闻(2019-10-06) 1. Go 1.14 有什么新变化 http://docs.goo ...