<div class="post"><h1 class="postTitle"><a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/mengdejun/p/mysql_group_order_by.html">mysql单列去重复group by分组取每组前几条记录加order by排序</a></h1><div class="clear"></div><div class="postBody"><div id="cnblogs_post_body"><p>mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下</p>

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

复制代码代码如下:

/* 
数据如下: 
name val memo 
a 2 a2(a的第二个值) 
a 1 a1--a的第一个值 
a 3 a3:a的第三个值 
b 1 b1--b的第一个值 
b 3 b3:b的第三个值 
b 2 b2b2b2b2 
b 4 b4b4 
b 5 b5b5b5b5b5 
*/

--创建表并插入数据:

复制代码代码如下:

create table tb(name varchar(10),val int,memo varchar(20)) 
insert into tb values('a', 2, 'a2(a的第二个值)') 
insert into tb values('a', 1, 'a1--a的第一个值') 
insert into tb values('a', 3, 'a3:a的第三个值') 
insert into tb values('b', 1, 'b1--b的第一个值') 
insert into tb values('b', 3, 'b3:b的第三个值') 
insert into tb values('b', 2, 'b2b2b2b2') 
insert into tb values('b', 4, 'b4b4') 
insert into tb values('b', 5, 'b5b5b5b5b5') 
go

--一、按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 
*/

本人推荐使用1,3,4,结果显示1,3,4效率相同,2,5效率差些,不过我3,4效率相同毫无疑问,1就不一样了,想不搞了。 
--二、按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的第一个值 
*/

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

复制代码代码如下:

select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name 
/* 
name val memo 
---------- ----------- -------------------- 
a 2 a2(a的第二个值) 
b 1 b1--b的第一个值 
*/

--四、按name分组随机取一条数据。

复制代码代码如下:

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--a的第一个值 
b 5 b5b5b5b5b5 
*/

--五、按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.valselect 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的第一个值 
a 2 a2(a的第二个值) 
b 1 b1--b的第一个值 
b 2 b2b2b2b2 
*/

--六、按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 2 a2(a的第二个值) 
a 3 a3:a的第三个值 
b 4 b4b4 
b 5 b5b5b5b5b5 
*/

--七,假如整行数据有重复,所有的列都相同(例如下表中的第5,6两行数据完全相同)。 
按name分组取最大的两个(N个)val

复制代码代码如下:

/* 
数据如下: 
name val memo 
a 2 a2(a的第二个值) 
a 1 a1--a的第一个值 
a 1 a1--a的第一个值 
a 3 a3:a的第三个值 
a 3 a3:a的第三个值 
b 1 b1--b的第一个值 
b 3 b3:b的第三个值 
b 2 b2b2b2b2 
b 4 b4b4 
b 5 b5b5b5b5b5 
*/

附:mysql “group by ”与"order by"的研究

这两天让一个数据查询难了。主要是对group by 理解的不够深入。才出现这样的情况

这种需求,我想很多人都遇到过。下面是我模拟我的内容表

我现在需要取出每个分类中最新的内容

select * from test group by category_id order by `date`

结果如下

明显。这不是我想要的数据,原因是msyql已经的执行顺序是

引用

写的顺序:select ... from... where.... group by... having... order by..
执行顺序:from... where...group by... having.... select ... order by...

所以在order by拿到的结果里已经是分组的完的最后结果。
由from到where的结果如下的内容。

到group by时就得到了根据category_id分出来的多个小组

到了select的时候,只从上面的每个组里取第一条信息结果会如下

即使order by也只是从上面的结果里进行排序。并不是每个分类的最新信息。
回到我的目的上 --分类中最新的信息
根据上面的分析,group by到select时只取到分组里的第一条信息。有两个解决方法
1,where+group by(对小组进行排序)
2,从form返回的数据下手脚(即用子查询)
由where+group by的解决方法
对group by里的小组进行排序的函数我只查到group_concat()可以进行排序,但group_concat的作用是将小组里的字段里的值进行串联起来。

select group_concat(id order by `date` desc) from `test` group by category_id

再改进一下

select * from `test` where id in(select SUBSTRING_INDEX(group_concat(id order by `date` desc),',',1) from `test` group by category_id ) order by `date` desc

子查询解决方案

select * from (select * from `test` order by `date` desc) `temp`  group by category_id order by `date` desc

mysql单列去重复group by分组取每组前几条记录加order by排序相关推荐

  1. mysql 排序取前4,mysql分组取每组前几条记录(排序)

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

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

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

  3. mysql自连接分组查询最新_MySQL 自连接分组取每组最大N条记录

    1.测试数据: create table t2 ( id int primary key, gid char, col1 int, col2 int ) engine=myisam; insert i ...

  4. SQL之Join的使用详解(附 :分组查询每组前N条记录)

    一.基本概念 关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释 –建表user1,user2: table1 : create table ...

  5. sql 取表的前10条记录,任意中间几行的记录

    取表的前10条记录 with a as(select *,row_number()over(order by department)rn from _SucceedStaff ) select * f ...

  6. MongoDB分组取每组中一条数据

    需求背景 有一个mongo collection,里面存放了运送货物的司机位置信息,字段主要有 _id: mongodb默认的主键字段 orderId:订单id positionTime:位置上报时的 ...

  7. 分组取每组的第一条或前N条

    情景:每天收集到很多意思相近的新闻,按照时间,取每天的第一条 1.group by 按照时间分组 select distinct substring(pubTime,1,10) pubTime,max ...

  8. mysql分组取出每组地一条数据_MySQL 分组后取每组前N条数据

    与oracle的rownumber() over(partition by xxxorder by xxx)语句类似,即:对表分组后排序 创建测试emp表 DROP TABLE IF EXISTS e ...

  9. SQL分组取每组前一(或几)条记录(排名)

    转自: https://www.cnblogs.com/netserver/p/4518995.html --按某一字段分组取最大(小)值所在行的数据 代码如下: /* 数据如下: name val ...

最新文章

  1. idea如何删除java里面工程,Java开发工具IntelliJ IDEA配置项目系列教程(七):卸载模块...
  2. Maven中使用tomcat:run 出现错误 org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException...
  3. 内存管理单元MMU学习
  4. Go包导入与Java的差别
  5. 论文Algorithms for non-negative matrix Factorization
  6. php 去掉字符串的最后一个字符
  7. 【c语言】用指针变量输出一维数组中的数据
  8. Ubuntu学习日记--Lesson2:创建、重命名、删除文件及文件夹,强制清空回收站方法
  9. TypeScript接口用法(基础)
  10. EPS数据导入CASS中
  11. 搭建Linux的基础命令符---bc
  12. 基于javaweb+springboot的电影售票系统(java+Springboot+ssm+mysql+jsp+maven)
  13. HG5520A型多用表校准仪
  14. SpringBoot + Thymeleaf 练手小项目 --------- 豆瓣网站模拟
  15. c#(WinForm)绘制两个圆的内公切线
  16. 目标检测各类数据集格式互转以及处理方法(VOC, COCO, txt)
  17. ​秋招上岸,机械转码经历和面经​
  18. Bhuman应用篇——带球及踢球
  19. 在vscode中打开.md文件
  20. 十年测试老鸟聊聊移动端兼容性测试

热门文章

  1. Leetcode题库 125.验证回文串(双指针 C实现)
  2. 概率论-3.3 多维随机变量函数的分布
  3. 实际电压/流源模型及其等效分析
  4. 设计模式大纲整理——编目、分类、选择与使用
  5. printf函数与主函数问题
  6. Protocol Buffer入门——轻松搭建java环境 .
  7. cvs update 的输出标志/update常用几个参
  8. Shell编程-控制结构 | 基础篇
  9. k8s服务网关ambassador部署
  10. linux定时备份mysql数据并同步到其他服务器