🌟博主主页:我是一只海绵派大星
📚专栏分类:软件测试笔记
📚参考教程:黑马教程
❤️感谢大家点赞👍收藏⭐评论✍️
目录
二、where 子句在 update 与 delete 语句中同样有效
3、当一条select语句出现了where和order by
一、空判断
- null不是0,也不是'',null在SQL里面代表空,什么也没有
- null不能用比较运算符的判断
- is null ---是否为null
- is not null ---是否不为null
不能用 字段名 = null 字段名 != null这些都是错误的
//查询 card 身份证为 null 的学生记录
SELECT * from students where card is null;
//查询 card 身份证非 null 的学生记录
SELECT * from students where card is not null;
二、where 子句在 update 与 delete 语句中同样有效
//修改 age 为 25,并且 name 为’孙尚香’的学生 class 为’2 班’
update students set class = '2班' where age = 25 and name = '孙尚香';
//删除 class 为’1 班’,并且 age 大于 30 的学生记录
DELETE from students where class = '1班' and age > 30;
三、order by排序
1、order by 字段名 [asc/desc]
- asc代表从小到大,升序,asc可以省略
- desc代表从大到小,不可以省略
//查询所有学生记录,按 age 年龄从小到大排序
select * from students order by age asc;
select * from students order by age;
//查询所有学生记录,按 age 年龄从大到小排序
select * from students order by age desc;
2、两个字段排序的例子
//查询所有学生记录,按 age 年龄从大到小排序,
//年龄相同时,再按 studentNo 学号从小到大排序
SELECT * from students ORDER BY age desc, studentNo;
3、当一条select语句出现了where和order by
- select * from 表名 where 条件 order by 字段1,字段2;
- 一定要把where写在order by前面
//studentNo 学号再按学号从大到小排序
SELECT * from students where sex = '男' order by class, studentNo desc;
四、聚合函数
聚合函数不能用到where后面的条件里
1、count求select返回的记录总数
- count(字段名)
//查询学生总数(查询stuents表有多少记录)
select count(*) from students;
select count(name) from students;
select count(DISTINCT class) from students;
select count(DISTINCT sex) from students;
//查询女同学数量
SELECT count(name) from students where sex = '女';
SELECT count(*) from students where sex = '女';
SELECT count(sex) from students where sex = '女';
2、max查询最大值
- max(字段名)
- 查询指定字段里的最大值
//查询students中的最大年龄
SELECT max(age) from students;
//查询students中的女生最大年龄
SELECT max(age) from students where sex = '女';
//查询students中的'1班'最大年龄
SELECT max(age) from students where class = '1班';
3、min查询最小值
- min(字段名)
- 查询指定字段的最小值
//查询students中的最小年龄
SELECT min(age) from students;
//查询students中的女生最小年龄
SELECT min(age) from students where sex = '女';
//查询students中的'1班'最小年龄
SELECT min(age) from students where class = '1班';
4、sum求和
- sum(字段名)
- 指定字段的值求和
//查询students中的年龄总和
SELECT sum(age) from students;
//查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';
//查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';
5、avg求平均数
- avg(字段名)
- 指定字段的平均值
//查询students中的年龄总和
SELECT sum(age) from students;
//查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';
//查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';
- avg的字段中如果有null,null不做为分母计算平均
create table aa (age int, name varchar(10));
insert into aa values (10, 'a'), (20, 'b'), (null, 'c');
select avg(age) from aa;//结果为15,而不是10
五、数据分组
- group by 字段名
- select 聚合函数 from 表名 where 条件 group by 字段
- select 聚合函数 from 表名 group by 字段
- group by就是配合聚合函数使用的
//分别查询男女同学的数量
SELECT count(*) from students where sex = '男';
SELECT count(*) from students where sex = '女';
select sex, count(*) from students group by sex;
- group by的例子
//分别查询各个年龄段的同学数量
select age, count(*) from students group by age;
- where与group by
//分别查询'1班'不同性别学生数量
select sex, count(*) from students where class = '1班' group by sex;
六、分组聚合之后的数据筛选
- having子句
- 总是出现在group by之后
- select * from 表名 group by 字段 having 条件
//用where查询男生总数
//where先筛选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex = '男';
//用having查询男生总数
//having先分组聚合统计,在统计的结果中筛选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';
1、having配合聚合函数的使用
- where后面条件不能使用聚合函数, having可以使用聚合函数
//求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) > 3;
七、having与where筛选的区别
- where是对标的原始数据进行筛选
- having是对group by之后已经分过组的数据进行筛选
- having可以使用聚合函数, where不能用聚合函数
八、limit显示指定的记录数
- select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
- limit总是出现在select语句的最后,
- start代表开始行号,行号从0开始编号
- count代表要显示多少行
- 省略start,默认从0开始,从第一行开始
//查询前三行记录
SELECT * from students limit 0, 3;
SELECT * from students limit 3;
//查询从第4条记录开始的三条记录
SELECT * from students limit 3, 3;
- 当有where或者group by或者order by, limit总是出现在最后
//查询年龄最大同学的name
select name from students ORDER BY age desc limit 1;
//查询年龄最小的女同学信息
SELECT * from students where sex = '女' ORDER BY age LIMIT 1;
九、数据分页显示
- m 每页显示多少条记录
- n,第n页
- (n - 1) * m, m
- 把计算结果写到limit后面
//每页显示4条记录,第3页的结果
select * from students limit 8, 4;
//每页显示4条记录,第2页的结果
select * from students limit 4, 4;
🎁结语:
本次精彩内容已圆满结束!希望各位读者在阅读过程中能够收获满满。在此,特别感谢各位读者的支持与三连赞。如果文章中存在任何问题或不足之处,欢迎在评论区留言,大星必定会认真对待并加以改进,以便为大家呈现更优质的文章。你们的支持与鼓励,将是博主不断前进的最大动力。再次感谢大家的陪伴与支持!