SQL语言基础(3)

聚合函数

count统计 

count(字段)

求 select 返回的记录总数

查询学生总数

select count(*) from studnets;

count可以结合distinct使用,去重后的统计

查询一共有多少个班级 (把班级进行去重后进行统计)

select count(distinct class) from students;

查询女学生的数量

select count(*) from studnets where sex ='女';

MAX 最大值

max(字段名)

查询指定字段里的最大值

查询students中的最大年龄

select max(age) from students;

查询女学生最大年龄

select max(age) from studnets where sex '女';

查询1班最大年龄

select max(age) from students where class = '1班';

MIN 最小值

min(字段)

查询指定字段里的最小值

查询学生中最小年龄

select min(age)  from students;

查询学生中女生最小年龄

select min(age) from studnets where sex ='女';

查询学生中1班最小年龄

select min(age) from students where class ='1班';

sun 求和

sum(字段名)

指定字段的值求和

查询学生的年龄总和

select sum(age) from students;

查询女学生的年龄总和

select sum(age) from students where sex = '女';

查询1班学生的年龄总和

select sum(age) from students where class ='1班';

AVG(平均值)

age(字段)表示求此字段的平均值

查询班级中的平均年龄

select avg(age) from  studnets;

查询班级中女生的平均年龄

select avg(age) from students where sex = '女';

查询班级为1班的平均年龄

select avg(age) from students where class ='1班';

avg的字段中如果有null,null不做为分母计算平均值 (如果所查询的3个值,其中一个值为null,而只能算做两个值求平均值)

扩展练习

查询所有学生的最大年龄,最小年龄,平均年龄;

select max(age),min(age),avg(age) from students;

查询1班有多少个学生

select count(*) from sutdnets where class = '1班';

查询3班中年龄小于30岁的同学有多少

select count(*) from students where class ='3班' and age < '30';

数据分组 group by

group by 字段名

select 聚合函数 from 表名 where 表名 group by 字段 

group by 就是配合聚合函数使用的

分别查询男女同学的数量

select sex,count(*) from students group by sex;

查询各个年龄段的学生数量

select age,count(*) from students group age;

查询1班男生、女生的数量

select sex,count(*) from where class ='1班' students group by sex;

用数据分组的方法,统计各个班级学生总数,平均年龄,最大年龄,最小年龄

select class count(*),avg(age),max(age),min(age) from students group by class;

统计各个班级的学生总数,平均年龄、最大年龄、最小年龄、但不统计3班的学生,统计结果按班级从小到大排序

select class count(*),avg(age),max(age),min(age) from students where class !='3班' group by class order by class desc;

分组以后筛选

用where查询男生总数

select count(*) from studnets where sex ='男';

用having查询男生总数

select count(*) from students group by sex having sex ='男';

求班级人数大于3人的班级

select class, count(*) from students group by class having count(*)> 3;

Having与where筛选的区别

where是对标的原始数据进行筛选

having是对group by之后已经分过组的数据进行筛选

having可以使用聚合函数 where不能用聚合函数

查询班级总人数大于2人的班级名称以及班级对应的总人数

select class,count(*) from students group by class having count(*) > 2;

查询平均年龄大于30岁的班级名称和班级总人数

select class,count(*) from students group by class having ave(age) > 30;

limit 显示指定的记录数

select * from 表名 where 条件 group by 字段 order by 字段 limit start,count 

 语法:limit开始行,获取行数 

limit总是出现在select语句的最后

start代表开始行号,行号从0开始编号

count代表要显示多少行

省略start默认从0开始,从第一行开始

查询前3行学生记录

select * from students limit 0,3;

查询从第4条记录开始的三条记录

select * from studnets limit 3,3;

当有where或者group by或者order by,limit总是出现在最后

查询年龄最大同学的名字

select name from students order by age desc limt 0,1;

查询年龄最小女同学的信息

select * from students where sex = 女 order by age  limit 0,1;

limit 数据分页显示

当一张表记录特别多的时候,就需要用到分页显示

已知:每页显示m条数据,求查询第n页数据

公式 limit (n-1)*m,m

每页显示4条记录,第3页的结果  

select * from students limit 8,4;

每页显示4条记录,第2页的结果

select * form students limit 4,4;

已知每页也数,求一张表需要几页显示

    ♦求总页数

    ♦总页数/每页的记录数

    ♦如果结果是整数,那么就是总页数,如果结果有小数,那么就在结果的整数上+1

每页显示5条记录,分别多条select显示每页的记录

第一页  

select * from limit 5;

第二页   套用公式 (n-1)*m,m  m条数 n页数

select * from limit 5,5;

第三页

select  * from limit 10,5; 

第四页

select * from limit 15,5;

你可能感兴趣的:(SQL语言基础(3))