mysql过滤表中重复数据,查询相同数据的特定一条
待操作的表如下:
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
select * from test_max_data;
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
+----+----------+-------------------+
| id | area | best_history_data |
+----+----------+-------------------+
| 1 | beijing | 33 |
| 2 | beijing | 22 |
| 3 | shanghai | 1 |
| 4 | shanghai | 2 |
| 5 | chengdu | 1 |
| 6 | chengdu | 2 |
| 7 | henan | 13 |
| 8 | henan | 12 |
+----+----------+-------------------+
期待:获取各个area中best_history_data最大的一条
1.最简单的一种方式,如果只需要从表中获取area和对应的最大best_history_data,可用以下方式:
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
select area,max(best_history_data) best_history_data from test_max_data group by area;
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
+----------+-------------------+
| area | best_history_data |
+----------+-------------------+
| beijing | 33 |
| chengdu | 2 |
| henan | 13 |
| shanghai | 2 |
+----------+-------------------+
该方式只能获取到area和对应的最大best_history_data字段值。
2.使用联表查询的方式
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
select a.* from test_max_data a join (select area,max(best_history_data) best_history_data from test_max_data group by area) b where a.area=b.area and a.best_history_data=b.best_history_data;
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
+----+----------+-------------------+
| id | area | best_history_data |
+----+----------+-------------------+
| 1 | beijing | 33 |
| 4 | shanghai | 2 |
| 6 | chengdu | 2 |
| 7 | henan | 13 |
+----+----------+-------------------+
3.使用相关子查询的方式
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
select * from test_max_data a where a.best_history_data=(select max(best_history_data) as best_history_data from test_max_data where area=a.area);
+----+----------+-------------------+
| id | area | best_history_data |
+----+----------+-------------------+
| 1 | beijing | 33 |
| 4 | shanghai | 2 |
| 6 | chengdu | 2 |
| 7 | henan | 13 |
+----+----------+-------------------+
4.使用not exists的方式
p.p1 { margin: 0; font: 16px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
select * from test_max_data a where not exists (select * from test_max_data b where a.area=b.area and a.best_history_data<b.best_history_data);
+----+----------+-------------------+
| id | area | best_history_data |
+----+----------+-------------------+
| 1 | beijing | 33 |
| 4 | shanghai | 2 |
| 6 | chengdu | 2 |
| 7 | henan | 13 |
+----+----------+-------------------+
参考:https://www.cnblogs.com/mianbaoshu/p/11821255.html
https://blog.csdn.net/Tenlay_Li/article/details/82704325
mysql过滤表中重复数据,查询相同数据的特定一条的更多相关文章
- select中DISTINCT的应用-过滤表中重复数据
在表中,一个列可能会包含多个重复值,有时也许希望仅仅列出不同(distinct)的值. DISTINCT 关键词用于返回唯一不同的值. SQL SELECT DISTINCT 语法 SELECT DI ...
- sql 查出一张表中重复的所有记录数据
1.在面试的时候碰到一个 问题,就是让写一张表中有id和name 两个字段,查询出name重复的所有数据,现在列下: select * from xi a where (a.username) in ...
- mysql删除表中重复数据,只保留一个最小的id的记录
语句: delete from table1 where id not in (select minid from (select min(id) as minid from table1 group ...
- mysql记录数据库中重复的字段的数据
SELECT SUM(co)FROM ( SELECT telephone, count(telephone) AS co ...
- 转 sql 查出一张表中重复的所有记录数据
select * from DB_PATCH awhere lower(a.db_name) in (select lower(db_name) from DB_PATCH group by lowe ...
- **SQL某一表中重复某一字段重复记录查询与处理
sql某一表中重复某一字段重复记录查询与处理 1.查询出重复记录 select 重复记录字段 form 数据表 group by houseno having count(重复记录字段)> ...
- sqlite 删除表中重复数据(亲测可用)
例子:表名 Paper .通过字段PaperID查找重复数据. 1 --查询某表中重复的数据 select * from Paper group by PaperID having co ...
- SQL Server 复制表结构以及数据,去除表中重复字段
--复制另一个数据库中的某张表的结构及数据--select * from Test.dbo.TestTable(查询表中所有数据) --into [表名] 插入当前数据库新表,如果没有该表就创建 se ...
- Sql Server删除数据表中重复记录 三种方法
本文介绍了Sql Server数据库中删除数据表中重复记录的方法. [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1 ...
随机推荐
- 校准仪开发日志--2017-10-20 today's question
- Linux下安装JDK 1.8你必须知道的糟心事
来源:Atstudy网校 1.简介 在Oracle收购Sun后,Java的一系列产品就被整合到Oracle官网中,打开官网乍眼一看也不知道去哪里下载,还的一个一个的摸索尝试,而且网上大多数都是一些Or ...
- 基于 element-plus 封装一个依赖 json 动态渲染的查询控件
前情回顾 基于 el-form 封装一个依赖 json 动态渲染的表单控件 Vue3 封装第三方组件(一)做一个合格的传声筒 功能 使用 vue3 + element-plus 封装了一个查询控件,专 ...
- week-01
week-01 1. 计算机组成 从底层开始: 硬件: CPU.内存.硬盘.网卡.主板.显卡.风扇.电源.鼠标键盘 等: 系统: Linux.Windows.Mac 等: 软件: QQ.微信.吃鸡.农 ...
- GO学习-(30) Go语言操作kafka
go操作kafka Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据,具有高性能.持久化.多副本备份.横向扩展等特点.本文介绍了如何使用Go语言发送和接收 ...
- RTOS诊断和错误检查
RTOS诊断和错误检查 RTOS diagnostics and error checking 查看RTOS显示系列 错误处理不太可能是任何用于嵌入式系统应用程序的操作系统的主要功能.这是资源限制的必 ...
- 什么是阻塞、非阻塞、同步和异步以及IO模型
首先先看如下几个问题,或者说我们经常会遇到的问题. 阻塞是否等于同步?非阻塞是否等于异步?同步一定是阻塞的么?异步一定是非阻塞的么?要把这四个概念讲明白,先从一顿简餐说起.假设你要做一顿便饭:烧土豆: ...
- Redis6.x学习笔记(五)哨兵
前言 最近学习Redis6.x,特做笔记以备忘,与大家共学.课程是从私塾在线下载的,他们把架构师课程都放出来了,大家可以去下载学习,不要钱的,地址是http://t.hk.uy/eK7,课程很不错,值 ...
- C# Net Core 使用 itextsharp.lgplv2.core 把Html转PDF
C# Net Core 使用 itextsharp.lgplv2.core 把Html转PDF 只支持英文(中文我不知道怎么弄,懂的朋友帮我看一下)!!!!![补充:评论区的小伙伴已解决] 引入包it ...
- 小目标增强(Augmentation for small object)
小物体检测的增强 摘要:在近些年来,目标检测已经有了长足的进步.尽管有很大改进,但是在小目标和大目标检测性能方面还是有巨大的差距.我们在具有挑战性的数据集MS-COCO上分析了目前性能最好的模型Mas ...