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 ...
随机推荐
- C语言函数sscanf()的用法-(转自Walter L)
在我的学习过程中,从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int ss ...
- 8.8-9 fsck、dd
8.8 fsck:检查并修复Linux文件系统 fsck命令用于检查并修复文件系统中的错误,即针对有问题的系统或磁盘进行修复,类似的命令还有e2fsck命令.有关fsck的使用需要特别注意的是: ...
- 9.13-15 runlevel & init & service
runlevel:输出当前运行级别 runlevel命令用于输出当前Linux系统的运行级别. -quiet 不输出结果,用于通过返回值判断的场合 [root@cs6 ~]# runlevel N 3 ...
- SQLZOO
一.SELECT basics/zh 以顯示德國 Germany 的人口. select population from world where name = 'Germany'; 查詢面積為 5,0 ...
- JavaScript 中精度问题以及解决方案
JavaScript 中的数字按照 IEEE 754 的标准,使用 64 位双精度浮点型来表示.其中符号位 S,指数位 E,尾数位M分别占了 1,11,52 位,并且在 ES5 规范 中指出了指数位E ...
- springboot——重定向解决刷新浏览器造成表单重复提交的问题(超详细)
原因:造成表单重复提交的原因是当我们刷新浏览器的时候,浏览器会发送上一次提交的请求.由于上一次提交的请求方式为post,刷新浏览器就会重新发送这个post请求,造成表单重复提交. 解决办法: 将请求当 ...
- 【python学习小知识】求绝对值和numpy和tensor的相互转换
一.python求绝对值的三种方法 1.条件判断 2.内置函数abs() 3.内置模块 math.fabs 1.条件判段,判断大于0还是小于0,小于0则输出相反数即可 # 法1:使用条件判断求绝对值 ...
- Java并发:乐观锁
作者:汤圆 个人博客:javalover.cc 简介 悲观锁和乐观锁都属于比较抽象的概念: 我们可以用拟人的手法来想象一下: 悲观锁:像有些人,凡事都往坏的想,做最坏的打算:在java中就表现为,总是 ...
- NX二次开发-获取WCS坐标系的原点坐标和矩阵标识
函数:UF_CSYS_ask_csys_info() 函数说明:获取工作坐标系对象的标识符. 用法: #include <uf.h> #include <uf_csys.h> ...
- Spring Boot 实战:如何自定义 Servlet Filter
1.前言 有些时候我们需要在 Spring Boot Servlet Web 应用中声明一些自定义的 Servlet Filter来处理一些逻辑.比如简单的权限系统.请求头过滤.防止 XSS 攻击等. ...