mysql分组和排序操作

时间:2021-11-07 01:34:56
分组、排序操作      
                                                                                
 select * from student order by id desc,height asc;                                                         排序,mysql默认根据主键正序排列,order by 排序字段,asc正序,desc倒序,防止一个条件相同内容导致无法排序,再增加第二个排序条件进行排序       
                           
 select count(*),gender from student group by gender;                                                   group by分组,以gender分组,select与from之间为打印出来的内容,分别统计男、女人数                          
 
select count(*),age from student where age = 20 group by age;                                  打印student表格里年龄为20的人数  
                            
 select count(*),age,group_concat(id) from student where age = 20 group by age;           
                      
 select gender,count(*) from student group by gender having count(*) > 3;                  student表格里以gender分组,打印gender与count个数,having必须跟group by使用,是在分组后二次筛选                          
 
select * from student limit 1,2;                                                                                         limit限制 1:从第二个开始,取2个数据   
                             
 select * from student order by rand() limit 1,2;                                                                order by rand()随机取数据         
                       
select gender,count(*) from student group by gender with rollup;                                       以gender分组,gender和count,   with rollup累加count