第五章:排序、聚合函数

一、排序

orderby

asc

  • 代表从小到大(1,2,3)
  • 升序
  • asc可以省略

desc

  • 代表从大到小(3,2,1
  • 倒序
  • 不可以省略
格式一:
select * from 表名 orderby 字段名 [asc/desc]
-- 查询所有学生记录,按age年龄从小到大排序
select * from students order by age asc;
select * from students order by age;

-- 例2:查询所有学生记录,按age年龄从大到小排序
select * from students order by age desc;

--排序的另一个例子
--例3:查询所有学生记录,按age年龄从大到小排序,
--年龄相同时,再按studentNo学号从小到大排序
SELECT * from students ORDER BY age desc, studeent No;
格式二:当一条select语句出现了where和order by;一定要把where写在orderby前面
select * from 表名 where 条件 order by 字段1, 字段2;


例子:查询所有男生记录,age从小到大,班级相同;
再按学号,从大到小
select * from students where sex = '男' order by age ,xuehao desc;

 

二、聚合函数

count

  • count求select返回记录的总数
格式一:select count(字段名) from 表名;
格式二:select count(字段名) from 表名 where 条件;

查询表中所有记录总数与select简单查询区别

可以先看下平常写法和count有什么不同

查询表中所有男生记录总数;
select * from students where sex= '男';

这里可以看到,所有男生信息都显示出来,并且记录有4条

这里用count查看,可以看到返回记录总数是4,所有count只是用来统计select查询中的总数

-- 查询学生中男生数量
select count(*) from students where sex= '男';

如果查询条件不变,字段变age,查询表中所有男生,年龄记录总数

这里看到记录还是4条

max(最大值)

  • 查询指定字段里的最大值
  • 聚合函数不能用到where后面的条件里
  • 计算数字型类型
格式一:select max(字段名) from 表名;
格式二:select max(字段名) from 表名 where 条件;

--查询students中的最大班级
SELECT max(class) from students;

--查询students中的男生最大年龄
SELECT max(age)from students where sex='男';

--查询students中的'1班'最大年龄
SELECT max(age) from students where class ='1班';

--查询students中的'1班'最大年龄的女生
select max(age) from students where class ='1班' and sex='女';

min(最小值)

  1. 查询指定字段里的最小值
  2. 聚合函数不能用到where后面的条件里
  3. 计算数字型类型
格式一:select min(字段名) from 表名;
格式二:select min(字段名) from 表名 where 条件;

--查询students中的最小班级
SELECT min(class) from students;

--查询students中的男生最小年龄
SELECT min(age)from students where sex='男';

sum(求和)

  • 计算数字型类型
  • 指定字段值总和
格式一:select sum(字段名) from 表名;
格式二:select sum(字段名) from 表名 where 条件;

--查询students中的1班级,女生年龄总和
SELECT sum(age)from students where sex='女' and class ='1班';

--查询students中的男生年龄总和
SELECT sum(age)from students where sex='男';

avg(平均值)

  • 指定字段值平均值
  • avg字段有null,不做为分母计算平均值
格式一:select sum(字段名) from 表名;
格式二:select sum(字段名) from 表名 where 条件;

--查询students中的1班级,女生年龄总和
SELECT sum(age)from students where sex='女' and class ='1班';

--查询students中的男生年龄总和
SELECT sum(age)from students where sex='男';

----------  练习  ----------

-- 查询所有学生最大、最小、平均年纪
select max(age),min(age),avg(age) from students;

-- 查询1班一共多少个学生
select count(*) from students where class ='1班';

-- 查询3班,年龄小于30有几个
select count(*) from students where class ='3班' and age < 30;

/* 查询班级在老家包含‘州‘,
且性别为女,年龄在27-35之间,的所有人数 */
select count(*) from students  where  hometown like '%州%' 
and sex = '女' or age between 27 and 35 ;

-- 查询身份证包含410,且姓名包含王或郭,的所有人数
select count(*) from students  where card like '%410%' 
and name like '%王%' or name like '%郭%';
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值