您现在的位置是:首页 > cms教程 > DedeCMS教程DedeCMS教程

mysql group by分组取每组前几条记录加order by排序方法

华章滕2023-09-24DedeCMS教程已有人查阅

导读mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下--按某一字段分组取较大(小)值所在行的数据复制代

mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下,按某一字段分组取最大(小)值所在行的数据;
复制代码代码如下:
/* 
数据如下: 
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

本文标签:

很赞哦! ()

相关源码

  • (PC+WAP)压缩机离心风机红色机械设备营销型网站pbootcms模板基于PbootCMS开发的压缩机/离心风机专用模板,助力机械设备企业构建高效营销平台;模板可编辑压缩机参数表、风机性能曲线等专业展示模块查看源码
  • (自适应)行业协会工会机构单位pbootcms网站源码本模板基于PbootCMS内核开发,为行业协会、工会组织及机构单位量身打造。采用响应式布局设计,可自动适配手机、平板等移动设备,数据实时同步更新。模板包含行业资讯查看源码
  • 响应式粉色美容整形化妆品pbootcms网站模板开源源码该网站模板为美容整形、化妆品企业设计,采用响应式布局确保在手机、平板及PC端自动适配显示效果。基于PbootCMS内核开发,支持一键替换图文内容快速转换至其他行业应用。查看源码
  • (自适应响应式)家电维修清晰服务网站pbootcms模板免费下载本模板基于PbootCMS内核开发,为维修服务类企业打造,特别适合家电维修、设备维护等行业使用。通过简洁直观的界面设计,帮助企业快速搭建专业级服务平台,实现线上业务高效管理。查看源码
  • 自适应建材瓷砖卫浴大理石类pbootcms网站模板源码下载为建材瓷砖、卫浴瓷砖企业打造的高端响应式门户模板,基于PbootCMS内核深度开发。采用前沿HTML5自适应架构,无缝兼容手机端触控交互与PC端展示场景。查看源码
  • (自适应响应式)html5蓝色智能水表营销型网站pbootcms模板下载PbootCMS内核开发,为智能水表企业打造的营销型网站解决方案,本模板基于PbootCMS内核开发,为智能水表及相关行业企业设计,采用HTML5+CSS3技术构建,具有响应式布局。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐