在数据库开发过程中,我们要为每种类型的数据取出前几条记录,或者是取最新、最小、最大等等,这个该如何实现呢,本文章向大家介绍如何实现mysql分组取最大(最小、最新、前N条)条记录。需要的可以参考一下。

先看一下本示例中需要使用到的数据

创建表并插入数据:

CREATE TABLE `tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`val` int(11) DEFAULT NULL,
`memo` varchar(20) CHARACTER SET latin1 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
insert into tb values('a',    2,   'a2');
insert into tb values('a', 1, 'a1');
insert into tb values('a', 3, 'a3');
insert into tb values('b', 1, 'b1');
insert into tb values('b', 3, 'b3');
insert into tb values('b', 2, 'b2');
insert into tb values('b', 4, 'b4');
insert into tb values('b', 5, 'b5');

数据表如下:

name val memo
a 2 a2
a 1 a1
a 3 a3
b 1 b1
b 3 b3
b 2 b2
b 4 b4
b 5 b5

按name分组取val最大的值所在行的数据

方法一:

select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name

方法二:

select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)

方法三:

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

方法四:

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

方法五:

select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name

方法六:

select * from (select * from tb ORDER BY val desc) temp GROUP BY name ORDER BY val desc;

以上六种方法运行的结果均为如下所示:

name val memo
a 3 a3
b 5 b5

小编推荐使用第一、第三、第四钟方法,结果显示第1,3,4种方法效率相同,第2,5种方法效率差些。

按name分组取val最小的值所在行的数据

方法一:

select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name

方法二:

select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)

方法三:

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

方法四:

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

方法五:

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
b 1 b1

按name分组取第一次出现的行所在的数据

sql如下:

select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
//这个是sql server的
//mysql应该是
select a.* from tb a where val = (select val from tb where name = a.name limit 1) order by a.name

结果如下:

name val memo
a 2 a2
b 1 b1

-----下面的没有验证-- 感觉是sql-server的写法,mysql的随机是rand(),前几条记录是limit N.

按name分组随机取一条数据

sql如下:

select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name

结果如下:

name val memo
a 1 a1
b 3 b3

按name分组取最小的两个(N个)val

第一种方法:

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val

第二种方法:

select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val

第三种方法:

select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name

结果如下:

name val memo
a 1 a1
a 2 a2
b 1 b1
b 2 b2

按name分组取最大的两个(N个)val

第一种方法:

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val

第二种方法:

select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val

第三种方法:

select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name

结果如下:

name val memo
a 3 a3
a 2 a2
b 5 b5
b 4 b4

转:http://www.manongjc.com/article/1082.html

同样问题:http://www.cnblogs.com/fps2tao/p/9038268.html

mysql分组取最大(最小、最新、前N条)条记录的更多相关文章

  1. mysql 分组取每个组的前几名的问题

    select *from hotel_addition_orders awhere (select count(*) from hotel_addition_orders where hotel_or ...

  2. mysql分组取每组前几条记录(排名)

    1.创建表 create table tb( name varchar(10), val int, memo varchar(20) ); 2.插入数据 insert into tb values(' ...

  3. mysql分组取topn

    本文来自  http://www.jb51.net/article/31590.htm 有些语句sql top n 是sqlserver语法 --按某一字段分组取最大(小)值所在行的数据 代码如下: ...

  4. sql 分组取每组的前n条或每组的n%(百分之n)的数据

    sql 分组取每组的前n条或每组的n%(百分之n)的数据 sql keyword: SELECT * ,ROW_NUMBER() OVER(partition by b.UserID order by ...

  5. mysql 分组取最新的一条记录(整条记录)

    方法:mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了  #select * from ...

  6. mysql分组取前N记录

    http://blog.csdn.net/acmain_chm/article/details/4126306 http://bbs.csdn.net/topics/390958705 1 我只用到了 ...

  7. mysql分组取每组前几条记录(排序)

    首先来造一部分数据,表mygoods为商品表,cat_id为分类id,goods_id为商品id,status为商品当前的状态位(1:有效,0:无效). CREATE TABLE `mygoods` ...

  8. MySQL分组查询每组最新的一条数据(通俗易懂)

    开发中经常会遇到,分组查询最新数据的问题,比如下面这张表(查询每个地址最新的一条记录): sql如下: -- ---------------------------- -- Table structu ...

  9. mysql分组取每组大的记录

    SELECT a.* FROM chat_log a INNER JOIN (SELECT MAX(id) id,to_user FROM chat_log GROUP BY to_user)b ON ...

随机推荐

  1. centos7下mail邮件的查看删除、禁止部分应用发邮件

    查看与删除 mail命令进入 & p                  #显示当前邮件& 2                  #显示标号为2的文件 & d 1-100     ...

  2. JDBC操作数据库的基本流程

    所有的JDBC应用程序都具有下面的基本流程: 1.加载数据库驱动并建立到数据库的连接. 2.执行SQL语句. 3.处理结果. 4.从数据库断开连接释放资源. 下面我们就来仔细看一看每一个步骤: 其实按 ...

  3. [BZOJ2125]最短路(圆方树DP)

    题意:仙人掌图最短路. 算法:圆方树DP,$O(n\log n+Q\log n)$ 首先建出仙人掌圆方树(与点双圆方树的区别在于直接连割边,也就是存在圆圆边),然后考虑点u-v的最短路径,显然就是:在 ...

  4. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  5. [Codeforces 23D] Tetragon

    Brief Intro: 给3条相同长度的边的中点,问是否存在一个严格凸四边形 Algorithm: 明显只要求出一个点就能利用对称性算出其他点的坐标 设中点K,L,M分别在边AB,BC,CD上,易知 ...

  6. 【计算几何】【状压dp】Codeforces Round #226 (Div. 2) D. Bear and Floodlight

    读懂题意发现是傻逼状压. 只要会向量旋转,以及直线求交点坐标就行了.(验证了我这俩板子都没毛病) 细节蛮多. #include<cstdio> #include<algorithm& ...

  7. 配置.net程序集搜索路径

    默认情况下,.net程序对外部程序集dll的搜索路径是exe文件所在的目录,虽然这种方式没有什么太多不好的地方,但是当我们引用外部程序集较多的时候显得非常杂乱.一种比较常用的解决方式是通过配置在app ...

  8. Android集成友盟社会化分享功能

    1.  产品概述 友盟社会化组件,可以让移动应用快速具备社会化分享.登录.评论.喜欢等功能,并提供实时.全面的社会化数据统计分析服务. 指南将会手把手教你使用社会化组件SDK,用5分钟为APP增加新浪 ...

  9. OpenGL ES2.0编程三步曲 -转

    原地址:http://blog.csdn.net/myarrow/article/details/7707943 1. 保存全局变量的数据结构 以下例子程序均基于Linux平台. typedef st ...

  10. iOS: ios视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller、ffmpeg-AVPlayer)

    介绍: 和音频播放一样,ios也提供个很多的API.如mediaPlayer.framework下的MPMediaPlayerController.AVFounditon.framework下的AVP ...