码迷,mamicode.com
首页 > 数据库 > 详细

mysql分组和排序操作

时间:2018-12-05 21:01:04      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:女人   限制   ffffff   ack   count   asc   group   之间   sql   

分组、排序操作      
                                                                                
 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                                
 

mysql分组和排序操作

标签:女人   限制   ffffff   ack   count   asc   group   之间   sql   

原文地址:https://www.cnblogs.com/sunzhiqi/p/10072692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!