测牛学堂:软件测试基础夯实之sql聚合函数总结

130 阅读1分钟

数据查询操作之排序

语法格式:

select * from 表名 order by 字段名 asc| desc 重点:

1 字段名可以有多个,如果字段名1 相同,再按照字段名2排序

2 默认情况下按照从小到大去排列

3 asc 就是从小到大排列 desc 从大到小排列

例子:查询所有学生信息,按年龄从小到大排序

select * from students order by age 

数据查询中的聚合函数

1使用聚合函数,可以方便的进行一些数据统计。

2 聚合函数不能作为where 的判断条件使用。 错误的: select * from students where age = max(age) XXX

3 常用的聚合函数:count() 查询总记录数 max() 查询最大值 min()查询最小值 sum() 求和 avg()求平均数

例1:查询students表中学生的总数:使用count聚合函数。

这里的name不是必须的,因为不管是* 还是name,age,都可以作为count的参数。

select count(name) from students;

例2 : 查询男生的最大年龄:用max聚合函数

select max(age) from students where sex = '男'

例3 :查询1班里面年龄最小的,使用min聚合函数

select min(age) from students where class=1

例4: 查询一班学生的年龄总和 使用sum聚合函数

select sum(age) from students where class = 1

例5:查询所有女生的平均年龄:使用avg聚合函数

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