博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 分组
阅读量:6207 次
发布时间:2019-06-21

本文共 3552 字,大约阅读时间需要 11 分钟。

表结构和数据

create table cat(      id(1) int not null auto_increment primary key,      cat_id int(1),      value int(1),      name varchar(20)  );  insert into cat (cat_id,name,value) values ('1','name1', '1');  insert into cat (cat_id,name,value) values ('1','name2', '2');  insert into cat (cat_id,name,value) values ('1','name3', '3');  insert into cat (cat_id,name,value) values ('1','name4', '4');  insert into cat (cat_id,name,value) values ('2','name5', '5');  insert into cat (cat_id,name,value) values ('2','name6', '6');  insert into cat (cat_id,name,value) values ('2','name7', '7');   insert into cat (cat_id,name,value) values ('2','name8', '8');  insert into cat (cat_id,name,value) values ('3','name9', '9');  insert into cat (cat_id,name,value) values ('3','name10','10');  insert into cat (cat_id,name,value) values ('3','name11','11');  insert into cat (cat_id,name,value) values ('3','name12','12'); mysql> select *from cat;+----+--------+-------+--------+| id | cat_id | value | name   |+----+--------+-------+--------+|  1 |      1 |     1 | name1  ||  2 |      1 |     2 | name2  ||  3 |      1 |     3 | name3  ||  4 |      1 |     4 | name4  ||  5 |      2 |     5 | name5  ||  6 |      2 |     6 | name6  ||  7 |      2 |     7 | name7  ||  8 |      2 |     8 | name8  ||  9 |      3 |     9 | name9  || 10 |      3 |    10 | name10 || 11 |      3 |    11 | name11 || 12 |      3 |    12 | name12 |+----+--------+-------+--------+12 rows in set (0.13 sec)

查询分组最大记录

// 默认取分组第一条mysql> select *from cat group by cat_id order by cat_id;+----+--------+-------+-------+| id | cat_id | value | name  |+----+--------+-------+-------+|  1 |      1 |     1 | name1 ||  5 |      2 |     5 | name5 ||  9 |      3 |     9 | name9 |+----+--------+-------+-------+3 rows in set (0.00 sec)mysql> select *from (select *from cat order by value desc) a group by cat_id;+----+--------+-------+--------+| id | cat_id | value | name   |+----+--------+-------+--------+|  4 |      1 |     4 | name4  ||  8 |      2 |     8 | name8  || 12 |      3 |    12 | name12 |+----+--------+-------+--------+3 rows in set (0.06 sec)mysql> select a.* from cat a where value = (select max(value) from cat where cat_id = a.cat_id) order by a.cat_id;mysql> select a.* from cat a,(select cat_id,max(value) value from cat group by cat_id) b where a.cat_id = b.cat_id and a.value = b.value order by a.cat_id; mysql> select a.* from cat a inner join (select cat_id, max(value) value from cat group by cat_id) b on a.cat_id= b.cat_id and a.value= b.value order by a.cat_id;+----+--------+-------+--------+| id | cat_id | value | name   |+----+--------+-------+--------+|  4 |      1 |     4 | name4  ||  8 |      2 |     8 | name8  || 12 |      3 |    12 | name12 |+----+--------+-------+--------+3 rows in set (0.00 sec)

分组前 3 条记录

mysql> select a.* from cat a where exists (select count(*) from cat where cat_id= a.cat_id and value > a.value having Count(*) < 3) order by a.cat_id,a.value desc;mysql> select *from cat a where (select count(*) from cat b where a.cat_id=b.cat_id and b.value>a.value) < 3 order by a.cat_id,a.value desc;+----+--------+-------+--------+| id | cat_id | value | name   |+----+--------+-------+--------+|  4 |      1 |     4 | name4  ||  3 |      1 |     3 | name3  ||  2 |      1 |     2 | name2  ||  8 |      2 |     8 | name8  ||  7 |      2 |     7 | name7  ||  6 |      2 |     6 | name6  || 12 |      3 |    12 | name12 || 11 |      3 |    11 | name11 || 10 |      3 |    10 | name10 |+----+--------+-------+--------+9 rows in set (0.15 sec)

转载地址:http://ggqca.baihongyu.com/

你可能感兴趣的文章
pdf 字体和图片抽取
查看>>
mybatis处理集合、循环、数组和in等语句的使用
查看>>
进入保护模式(三)内存的分页
查看>>
hmailserver批量添加用户
查看>>
SaltStack源码分析之:master端执行salt模块大致流程
查看>>
Javascript验证上传图片大小[前台处理]
查看>>
mac os x10.8下如何使用git与github
查看>>
iOS-FMDB
查看>>
redis简单学习3-redis常用命令总结
查看>>
hive 集成sentry
查看>>
mysql日志(介绍 路径修改 备份)
查看>>
git 配置图形比较工具
查看>>
SpringAPI手动创建代理对象——ProxyFactory
查看>>
iOS--数据存储NSUserDefaults
查看>>
Yii2.0 ActiveForm Input Fields
查看>>
navicat连接oracle 报 ORA-12737 set CHS16GBK
查看>>
linux下共享文件夹(windows可访问,linux也可访问)
查看>>
JavaScript-基础入门.0014.JavaScript内置对象
查看>>
Python+Flask.0010.FLASK即插视图之自定义视图类及修饰器
查看>>
javax.validation.ValidationException: Unable to find a default provider
查看>>