MySQL取每组的前N条记录

一、对分组的记录取前N条记录:例子:取前 2条最大(小)的记录

 1 1.用子查询:
 2 SELECT * FROM right2 a  WHERE 2>
 3 (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account>a.account)
 4 ORDER BY a.id,a.account DESC
 5 2.用exists半连接:
 6 SELECT * FROM right2 a  WHERE EXISTS
 7 (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND a.account<b.account HAVING COUNT(*)<2)
 8 ORDER BY a.id,a.account DESC
 9 同理可以取组内最小的N条记录:
10 SELECT * FROM right2 a  WHERE 2>
11 (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account<a.account)
12 ORDER BY a.id,a.account DESC
13 用exists:
14 SELECT * FROM right2 a  WHERE EXISTS
15 (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND a.account>b.account HAVING COUNT(*)<2)
16 ORDER BY a.id,a.account DESC

如果取每组的最大(小)一条记录我常用:

1 select id,val from t b inner join(select * from t a where  order by val desc) a  on a.id=b.id group by a.id order  by id;

二.实例:取每组最大的前 N条

 1 create table t2 (
 2   id int primary key,
 3   gid char,
 4   col1 int,
 5   col2 int
 6 ) engine=innodb;
 7 insert into tx01 values
 8 (1,‘A‘,31,6),
 9 (2,‘B‘,25,83),
10 (3,‘C‘,76,21),
11 (4,‘D‘,63,56),
12 (5,‘E‘,3,17),
13 (6,‘A‘,29,97),
14 (7,‘B‘,88,63),
15 (8,‘C‘,16,22),
16 (9,‘D‘,25,43),
17 (10,‘E‘,45,28),
18 (11,‘A‘,2,78),
19 (12,‘B‘,30,79),
20 (13,‘C‘,96,73),
21 (14,‘D‘,37,40),
22 (15,‘E‘,14,86),
23 (16,‘A‘,32,67),
24 (17,‘B‘,84,38),
25 (18,‘C‘,27,9),
26 (19,‘D‘,31,21),
27 (20,‘E‘,80,63),
28 (21,‘A‘,89,9),
29 (22,‘B‘,15,22),
30 (23,‘C‘,46,84),
31 (24,‘D‘,54,79),
32 (25,‘E‘,85,64),
33 (26,‘A‘,87,13),
34 (27,‘B‘,40,45),
35 (28,‘C‘,34,90),
36 (29,‘D‘,63,8),
37 (30,‘E‘,66,40),
38 (31,‘A‘,83,49),
39 (32,‘B‘,4,90),
40 (33,‘C‘,81,7),
41 (34,‘D‘,11,12),
42 (35,‘E‘,85,10),
43 (36,‘A‘,39,75),
44 (37,‘B‘,22,39),
45 (38,‘C‘,76,67),
46 (39,‘D‘,20,11),
47 (40,‘E‘,81,36);
48 create table tx01 (
49   id int primary key,
50   gid char,
51   col1 int,
52   col2 int
53 ) engine=innodb;

取每组gid 最大的前N条记录:使用自连接或则半连接

*N=1时:

自连接:降序排好后group by取每组最大的一条。

select * from (select * from t2 order by col2 desc)as a group by gid order by gid;

半连接方式:找不到比最大值还大的。

select * from t2 a where not exists(select 1 from t2 b where b.gid=a.gid and b.col2>a.col2) order by a.gid; 

*N=3时:

自连接:

select * from t2 a where 3>(select count(*) from t2 where gid=a.gid and col2>a.col2) order by a.gid,a.col2 desc;

半连接:

select * from t2 a where exists(select count(*) from t2 b where b.gid=a.gid and a.col2<b.col2 having(count(*))<3) order by a.gid,a.col2 desc

转:本文出自 http://huanghualiang.blog.51cto.com/6782683/1252630

时间: 2024-10-11 22:12:01

MySQL取每组的前N条记录的相关文章

mysql查询各种类型的前N条记录

mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可  (select * from event_info where event_type = 1  limit 3)union all(select * from event_info where event_type = 2  limit 3)union all(select * from event_info where event_type = 3  limit 3) 原文出处:http://my.oschina.net/u/

sqlserver--获取同组数据的前两条记录

不啰嗦,直接上图,大概实现效果如下: 有上面这样一份数据,将他们按照userAccount和submitTime进行分组,然后提前每组数据的前两条记录 提取后数据如下: 实现的SQL如下: select t.* from (select *,row_number() over(partition by userAccount, submitTime order by submitTime) rn from demoTable) t where rn <=2 @[email protected]!

sqlServer 取每组的前几条数据

首先的建表语句: if exists (select * from sysobjects where id = OBJECT_ID('[test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) DROP TABLE [test] CREATE TABLE [test] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [nvarchar] (50) NULL , [votenum] [int] NULL ,

MySQL分组排序取前N条记录以及生成自动数字序列--group by 后 limit 外加 rownumber

同事提了一个需求,要求按照某列分组,然后将各组的前几条抽取出来. 表结构 CREATE TABLE `total_freq_ctrl` ( `time` int(10) unsigned NOT NULL, `machine` char(64) NOT NULL, `module` char(32) NOT NULL, `total_flow` int(10) unsigned NOT NULL, `deny_flow` int(10) unsigned NOT NULL, PRIMARY KE

mysql使用GROUP BY分组实现取前N条记录的方法

MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道怎么想的,当时表名直接敲了个aa,汗~~~~): 结果: 方法一: SELECT a.id,a.SName,a.ClsNo,a.Score FROM aa a LEFT JOIN aa b ON a.ClsNo=b.ClsNo AND a.Score<b.Score group by a.id,a.

[MySQL] 分组排序取前N条记录以及生成自动数字序列,类似group by后 limit

前言:         同事的业务场景是,按照cid.author分组,再按照id倒叙,取出前2条记录出来.        oracle里面可以通过row_number() OVER (PARTITION BY cid,author ORDER BY id DESC) 表示根据cid,author分组,在分组内部根据id排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的),而mysql数据库就没有这样的统计函数,需要自己写复杂的sql来实现. 1,录入测试数据 USE csd

MySQL每个分类的前几条记录

MySQL 获取所有分类和每个分类的前几条记录 比如有文章表 Article(Id,Category,InsertDate) 现在要用SQL找出每种类型中时间最新的前N个数据组成的集合 SELECT A1.* FROM Article AS A1 INNER JOIN (SELECT A.Category,A.InsertDate FROM Article AS A LEFT JOIN Article AS B ON A.Category = B.Category AND A.InsertDat

[mysql] 查询前几条记录

From: http://www.cnblogs.com/xuxm2007/archive/2010/11/16/1878211.html SELECT   *   FROM   table   LIMIT   5;           select     *     from     issu_info     limit     0,6             Limit     0,6       这里是对的,显示前6条                   select     *   

db2中取表的前几条记录 (转)

原文出处:http://csjava.blog.163.com/blog/static/19047003320119110358572/ db2中SELECT TOP N * FORM TABLENAME 是不能取到表中前几条记录的.要使用 SELECT * FROM TABLENAME FETCH FIRST N ROWS ONLY.其中N即是你要取的前几条记录. db2中取表的前几条记录 (转)