登录
首页 >  数据库 >  MySQL

mysql 分组

来源:SegmentFault

时间:2023-01-16 10:44:20 245浏览 收藏

积累知识,胜过积蓄金银!毕竟在##column_title##开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《mysql 分组》,就带大家讲解一下MySQL、PHP知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

表结构和数据

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(*)  select *from cat a where (select count(*) from cat b where a.cat_id=b.cat_id and b.value>a.value) 

好了,本文到此结束,带大家了解了《mysql 分组》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表