mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据
mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结几种mysql查询最大值 mysql查询最小值的方法!
mysql表图如下

具体Php 连接mysql数据库php代码我就不写 下面看select怎么查询
一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/
二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/
这种分组方法好像效率不是很高,我用了10000条记录,5个分组实验一下,要70s的查询时间。但对一般可以满足需求的
mysql按某一字段分组取最大(小)值所在行的数据的更多相关文章
- SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...
- MySQL在按照某个字段分组、排序加序号
事情是这样的,最近领导给了一个新的需求,要求在一张订单表中统计每个人第一次和第二次购买的时间间隔,最后还需要按照间隔统计计数,求出中位数等数据. 由于MySQL不想Oracle那般支持行号.中位数等, ...
- oracle 根据字段分组取第一条数据及rank函数说明
当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...
- MySql按字段分组取最大值记录 [此博文包含图片]
要求:获得按table1_id分组,并且age最大的记录信息,即2.3.5条 方法一: select * from (select * from table2 order by age d ...
- MySQL 先按某字段分组,再取每组中前N条记录
按 gpcode每组 取每组 f4 最大的那条记录: 方法一: select * from calcgsdataflash a where gscode = 'LS_F' and ymd >= ...
- MySql按字段分组取最大值记录
数据库原始数据如下:数据库名:tbl_clothers 需求是:按照type分组,并获取个分组中price中的最大值,解决sql如下: 方法一: select * from (select type, ...
- [mysql] 先按某字段分组再取每组中前N条记录
From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/33002126 ...
- mysql语句求按字段分组后组数是多少
select count(distinct ID) from table Thinkphp CURD写 $count = $model->where($where)->count('dis ...
- 【MySQL作业】多字段分组和 having 子句——美和易思分组查询应用习题
点击打开所使用到的数据库>>> 1.按照商品类型和销售地区分组统计商品数量和平均单价,并按平均单价升序显示. -- 按照商品类型和销售地区分组统计商品数量和平均单价,并按平均单价升序 ...
随机推荐
- SGU 206. Roads
206. Roads time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output: sta ...
- C# 6.0 新特性 (二)
自动属性初始化表达式 有过正确实现结构经验的所有 .NET 开发人员无疑都为一个问题所困扰:需要使用多少语法才能使类型固定不变(为 .NET 标准建议的类型).此问题实际上是只读属性存在的问题: 定义 ...
- Java String class methods
Java String class methods 现在不推荐使用 StringTokenizer 类.建议使用 String 类的 split()方法或 regex(正则表达式). String c ...
- karma+requirejs
下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/ 目录结构 步骤 安装 npm install ...
- Python并发编程-生产消费模型
生产消费模型初步 #产生两个子进程,Queue可以在子进程之间传递消息 from multiprocessing import Queue,Process import random import t ...
- React篇章-React 组件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- 树论讲解——最近公共祖先(lca)
最近公共祖先?! 有人肯定要问:什么是最近公共祖先???!! 好那我们现在就来说说什么是最近公共祖先吧! 最近公共祖先有一个好听的名字叫——lca 这是一种算法,这个算法基于并查集和深度优先搜索.算法 ...
- 在phpWeChat里生成一个临时二维码(非微信二维码)
phpWeChat作为支持Pc+H5开发的管理系统,内置一套二维码生成API,访问地址: 您的域名/api/qrcode/index.php?data=二维码数据 以下为使用示例 使用时有一点需要注意 ...
- 请画出Servlet 2.2以上Web Application的基本目录结构
Java web工程下的webapp或WebContent就是工程的发布文件夹,发布时会把该文件夹发布到tomcat的webapps里. 一个web应用必须要有的目录文件如下: webapp/WebC ...
- navicat批量导入数据
1.excel表导入数据 根据数据表添加excel表内容 开始导入数据