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按某一字段分组取最大(小)值所在行的数据的更多相关文章

  1. SQL Server 按某一字段分组 取 最大 (小)值所在行的数据

    SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...

  2. MySQL在按照某个字段分组、排序加序号

    事情是这样的,最近领导给了一个新的需求,要求在一张订单表中统计每个人第一次和第二次购买的时间间隔,最后还需要按照间隔统计计数,求出中位数等数据. 由于MySQL不想Oracle那般支持行号.中位数等, ...

  3. oracle 根据字段分组取第一条数据及rank函数说明

    当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...

  4. MySql按字段分组取最大值记录 [此博文包含图片]

      要求:获得按table1_id分组,并且age最大的记录信息,即2.3.5条     方法一: select * from (select * from table2 order by age d ...

  5. MySQL 先按某字段分组,再取每组中前N条记录

    按 gpcode每组 取每组 f4 最大的那条记录: 方法一: select * from calcgsdataflash a where gscode = 'LS_F' and ymd >= ...

  6. MySql按字段分组取最大值记录

    数据库原始数据如下:数据库名:tbl_clothers 需求是:按照type分组,并获取个分组中price中的最大值,解决sql如下: 方法一: select * from (select type, ...

  7. [mysql] 先按某字段分组再取每组中前N条记录

    From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/33002126 ...

  8. mysql语句求按字段分组后组数是多少

    select count(distinct ID) from table Thinkphp CURD写 $count = $model->where($where)->count('dis ...

  9. 【MySQL作业】多字段分组和 having 子句——美和易思分组查询应用习题

    点击打开所使用到的数据库>>> 1.按照商品类型和销售地区分组统计商品数量和平均单价,并按平均单价升序显示. -- 按照商品类型和销售地区分组统计商品数量和平均单价,并按平均单价升序 ...

随机推荐

  1. cfg 4 ocl

    http://blog.sina.com.cn/s/blog_6c868c470102v15y.html rnnlib真难装 http://sourceforge.net/p/rnnl/wiki/Ho ...

  2. 洛谷P1554 梦中的统计 题解

    题目传送门 这道题暴力又让我过了...数据真的很水(luogu) 暴力枚举n~m的每个数,再统计一次,交付评测...AC #include<bits/stdc++.h> using nam ...

  3. EF – 6.一对一关联

    5.6.6 <一对一关联概述>  5.6.7 <一对一关联CRUD演示>  在两讲视频中,首先介绍了数据库中一对一关联表的设计规范,接着通过实例介绍了如何合适Entity Fr ...

  4. electron 使用中的注意事项

    一.ELECTRON引用JQUERY.JS electron不能像正常的html文件引用jq.js那样(为嘛不造),elecron引用jq.js的方式为: <script>window.$ ...

  5. 【PAT】1001. A+B Format (20)

    1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...

  6. python通过帐号和密码访问mysql

    数据库:test1  数据表:a1 用input输入姓名和密码,对了的话显示信息.不对的话显示error import pymysql import hashlib conn = pymysql.co ...

  7. bzoj 1116

    思路:每个连通块都判是否有环. #include<bits/stdc++.h> #define LL long long #define fi first #define se secon ...

  8. oracle创建简单的包

    --规范 create or replace package test_pkg is --test_pkg为包名 procedure showMessage; --声明一个过程 function my ...

  9. Ubuntu编译安装nginx,php,mysql

    摘要: 整理的Ubuntu编译安装nginx,php,mysql的步骤,主要来自对驻云的sh-1.4.1中脚本的整理,随时代进步,内容中的软件或者命令请自行更新 目录准备 创建用户 userdel w ...

  10. JAVA编程思想读书笔记(一)--面向对象

    第二章 一切都是对象 No1: 程序运行时,我们最好对数据保存到什么地方做到心中有数.特别要注意的是内存的分配.有六个地方都可以保存数据: (1) 寄存器.这是最快的保存区域,因为它位于和其他所有保存 ...