天天看点

mysql排序以及分组查询巩固(含题型)

Order by 的使用

升序:asc;

降序:desc降序;

如果不指定asc或者desc,结果集默认按ASC进行排序;

语法格式:

select字段名1,字段名2,….
From 表名
Orderby字段名1[asc|desc],字段名2[asc|desc]…      

查询sc表的内容,要求按照成绩降序排列;

order by:排序

语法结构:order by+该字段 [asc|desc]:asc(升序排列,省略) desc(降序排列,desc不能升序)

select * from sc order by degree desc;      

(1)order by 可以对单个列进行排序

例1:查询sc表的内容,并按照成绩降序排列;

例2:查询student表里的sbirthday字段,并按照年龄进行降序排序;

select sbirthday from student order by sbirthday asc;
select sbirthday from student order by timestampdiff(year,sbirthday,now()) desc;      

(2)order by对多个列进行排序

例1:查询sc表内容,按照课程递增、成绩递减进行排序;

select * from sc order by cno asc,degree desc;      

可以发现:那个字段在前面,先按照那个字段进行排序;然后再进行第二个字段排序优先规则:哪个字段在前面,先排序哪个字段;再去排后面的字段;

例2:查询teaching表,按照课程号(cno)递增、学期(cterm)递增进行排序;

select * from teaching order by cno asc,cterm asc;      

使用顺序:

Order by 当结果集出来之后就可以进行排序,即:where → group by → having → order by

limit(限制条目)在语句的最后,起到限制的作用

分组查询

一个电器销售电销售洗衣机、冰箱、电视剧等,到了月份需要分类统计洗衣机、冰箱、电视机销售总数。像这种,对数据进行统计按照一定的类别进行统计,我们都需要使用分组查询;

分组查询:group by,需要注意的是,group by 通常是和聚合函sum、avg、count一块使用;

问题1:查询sc表每个学生的平均成绩

select avg(degree) from sc;      

问题2:查询sc表里每门课程的平均的成绩;

select avg(degree) from sc group by cno;      

问题3:查询student表里每个系有多少名学生;

select count(*) from student group by sdept;      

问题4:查询student表里男生和女生分别有多少人;

select count(*) from student group by ssex;      

多个字段进行分组

例1:查询每个系男生、女生分别有多少人?

select sdept,ssex,count(*) from student group by sdept,ssex;      

例2:查询每个系,每个班分别有男生和女生多少人?

select sdept,classno,ssex,count(*) from student group by sdept,classno,ssex;      

group by 字句也和where条件语句结合在一起使用。当结合在一起时,where在前,group by 在后。即先对select xx from xx的记录集合用where进行筛选,然后再使用group by 对筛选后的结果进行分组 ;

注意点:

1.where肯定在group by 之前

2.where后的条件表达式里不允许使用聚合函数,而having可以。

逻辑:where子句先筛选,筛选后再用这个group by进行分组;

位置上一致: where-group by

group by和where子句一块使用

例1:查询每个系男生有多少人?

select sdept,ssex,count(*) from student where ssex="男" group by sdept;      

where 先进行筛选,然后group by 再进行分组;

例2:查询每个系年龄不大于40岁的有多少人;

select count(*) from student 
where timestampdiff(year,sbirthday,now())<=40 
group by sdept;

select sdept,count(*) from student where 
timestampdiff(year,sbirthday,now())<=40 
group by sdept;      

题型综合

1、查询sc表里每门课程的选修人数;

分组查询的关键点:找到分组字段

count:统计有多少行,每一行记录代表一个人

select cno,count(*) from sc group by cno;      

2、查询sc表里每个学生的总成绩;

select sno,sum(degree) from sc group by sno;
select sum(degree) from sc;      

3、查询teacher表里每个系分别有多少男老师和女老师;

group by:不仅单个字段进行分组,一次性可以对多个字段进行分组

select tdept,tsex,count(*) from teacher group by tdept,tsex;      

4、查询teacher表里每个系年龄不超过50岁的女老师有多少人;

select tdept,count(*) from teacher 
where timestampdiff(year,tbirthday,now())<=50 and tsex="女" 
group by tdept;      

5、查询teacher表里男老师和女老师分别有多少人

select tsex,count(*) from teacher group by tsex;      

6、查询student表里每个系每个专业分别有多少男生和女生?(注:其中student表里speciality字段表示专业)

group by 字段1,字段2,…字段n;

select sdept,speciality,ssex,count(*) from student group by sdept,speciality,ssex;      

7、查询student表里每个专业年龄在32-40岁的女学生有多少人

select speciality,count(*) from student 
where timestampdiff(year,sbirthday,now()) between 32 and 40 and ssex="女" 
group by speciality;      

8、查询sc表里学号在2007010101-2007030111之间的每个学生的平均成绩,并按照平均成绩降序排列;

select sno,avg(degree) from sc 
where sno between 2007010101 and 2007030111 
group by sno 
order by avg(degree) desc;      
select sdept,count(*) from student 
where sdept in("信息工程系","软件工程系","计算机工程系") 
group by sdept;      

继续阅读