MySQL取每组的前N条记录
一、对分组的记录取前N条记录:例子:取前 2条最大(小)的记录
.用子查询:
SELECT * FROM right2 a WHERE >
(SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account>a.account)
ORDER BY a.id,a.account DESC
.用exists半连接:
SELECT * FROM right2 a WHERE EXISTS
(SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND a.account<b.account HAVING COUNT(*)<)
ORDER BY a.id,a.account DESC
同理可以取组内最小的N条记录:
SELECT * FROM right2 a WHERE >
(SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account<a.account)
ORDER BY a.id,a.account DESC
用exists:
SELECT * FROM right2 a WHERE EXISTS
(SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND a.account>b.account HAVING COUNT(*)<)
ORDER BY a.id,a.account DESC
如果取每组的最大(小)一条记录我常用:
select t1.* from table t inner join(select * from table t1 order by id desc) t1 on t.id=t1.id group by t.id order by t.id; //一定用t1.*,用t.*不OK
二.实例:取每组最大的前 N条
create table t2 (
id int primary key,
gid char,
col1 int,
col2 int
) engine=innodb;
insert into tx01 values
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,),
(,'A',,),
(,'B',,),
(,'C',,),
(,'D',,),
(,'E',,);
create table tx01 (
id int primary key,
gid char,
col1 int,
col2 int
) 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 from t2 b where b.gid=a.gid and b.col2>a.col2) order by a.gid;
*N=3时:
自连接:
select * from t2 a where >(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(*))<) order by a.gid,a.col2 desc
转:本文出自 http://huanghualiang.blog.51cto.com/6782683/1252630
MySQL取每组的前N条记录的更多相关文章
- sql 用Group by分组后,取每组的前几条记录
转自:http://blog.163.com/jeson_lwj/blog/static/135761083201052411115783/ --查询每门课程的前2名成绩 CREATE TABLE S ...
- 用Group by分组后,取每组的前3条记录,怎么取?
使用子查询进行查询 SELECT * FROM home_content a WHERE ( SELECT count(id) FROM home_content WHERE class_link = ...
- sql 分组取每组的前n条或每组的n%(百分之n)的数据
sql 分组取每组的前n条或每组的n%(百分之n)的数据 sql keyword: SELECT * ,ROW_NUMBER() OVER(partition by b.UserID order by ...
- sql 分组后显示每组的前几条记录
sql 分组后显示每组的前几条记录 如 表中记录是 code serialno A1 1 ...
- mysql查询各种类型的前N条记录
mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可 (select * from event_info where event_type = 1 limit 3)union all( ...
- mysql获取所有分类的前n条记录的两种方法浅析
项目中有个需求是获取出展会分类下的前n条展商. 刚开始的思路是用group by 获取出展会的分类,后面再根据分类获取该分类下的n个展商,但也需要第一次获取出展会的时候也获取所有的每个展会分类下的 ...
- sqlServer 取每组的前几条数据
首先的建表语句: ) DROP TABLE [test] CREATE TABLE [test] ( [id] [, ) NOT NULL , [name] [nvarchar] () NULL , ...
- mysql单列去重复group by分组取每组前几条记录加order by排序
mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下 --按某一字段分组取最大(小)值所在行的数据 复制代码代码如下: /* 数据如下: name ...
- mysql使用GROUP BY分组实现取前N条记录的方法
MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道 ...
随机推荐
- java-首字母大小写
/** * 首字母小写 * * @param str * @return */ public static String toLowerCaseFirstChar(String s) { if (Ch ...
- Web前端性能优化教程07:精简JS 移除重复脚本
本文是Web前端性能优化系列文章中的第七篇,主要讲述内容:精简Javascript代码,以及移出重复脚本.完整教程可查看: 一.精简javascript 基础知识 精简:从javascript代码中 ...
- pycharm使用错误排查
1.pip安装扩展包报错 creating build/temp.linux-x86_64-3.4 x86_64-linux-gnu- -Wformat -Werror=format-security ...
- ActiveMQ;RabbitMQ;ZeroMQ
中间件类型: Embedded middleware: As the name suggests, this typeof middleware handles embedded applicatio ...
- GraphX 的属性图
package main.scala import org.apache.spark.graphx.{Edge, Graph, VertexId} import org.apache.spark.rd ...
- Leetcode Power of two, three, four
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- javascript要不要用来处理业务逻辑
这个问题,我现在才明白该怎样描述这个已经困惑了我很久的问题,在公司做的项目中,js对于数据验证起到了很大的作用,但是这使得整个项目的业务逻辑变得零散,甚至混乱 现在公司里的项目就是这样的.现在再想想, ...
- PowerDesigner设计表时显示注释选项
选定编辑的表,[右键]->[Properties]->[Columns]->[Customize Columns and Filter]->[Comment]->[OK] ...
- 重新认识unicode和utf8编码
重新认识unicode和utf8编码 直到今天,准确的说是刚才,我才知道UTF-8编码和Unicode编码是不一样的,是有区别的囧 他们之间是有一定的联系的,看看他们的区别: UTF-8的长度是不一定 ...