MySQL学习总结(四):数据记录的查询

数据记录的查询

数据准备

create table exam(
		id int primary key auto_increment,
		name varchar(20) not null,
		chinese double,
		math double,
		english double
);
insert into exam values(null,'关羽',85,76,60);
insert into exam values(null,'张飞',70,75,70);
insert into exam values(null,'赵云',90,65,95);
insert into exam values(null,'刘备',97,50,50);
insert into exam values(null,'曹操',90,89,80);
insert into exam values(null,'司马懿',90,67,65);

1、查询exam表中所有学生的信息

select * from exam;

2、查询exam表中所有学生的姓名和对应的英语成绩

select name, english from exam;

3、查询姓名为赵云的学生成绩

select name, english from exam where name = ‘赵云’;

在这里插入图片描述
4、查询英语成绩大于90分的同学

select * from exam where english > 90;

5、查询英语分数不等于70分的所有同学

select * from exam where english <> 70;

6、查询英语分数在 80-90之间的同学(包含80和90)

 select * from exam where english between 80 and 90;

7、查询数学分数为89,75,91的同学

select * from exam where math in(89,75,91);

8、查询所有姓刘的学生成绩

 select * from exam where name like '刘%';

9、查询所有姓刘两个字的学生成绩

select * from exam where name like ‘刘_’;

10、查询数学成绩不为null的学生

 select * from exam where math is not null;

11、查询数学成绩为null的学生.

select * from exam where math is null;

12、查询数学分>80并且语文分>80的同学

select * from exam where math > 80 and chinese > 80;

13、查询数学分>80 或者 语文分>80的同学

select * from exam where math > 80 or chinese > 80;

14、查询英语分数不大于60的学生

select * from exam where not english > 60;

15、单列虑重

select distinct 列1 from table;

16、多列虑重:需要两列一模一样才会虑重

select distinct 列1,列2 from table;

17、过滤掉重复的语文成绩

select distinct chinese from exam;

18、别名

select id as '编号', name as '姓名', chinese as '语文', math as '数学', english as '英语' from exam;

19、别名省略模式

as 也可以省略 。 (不建议省略)  select id 编号, name 姓名, chinese 语文, math 数学, english 英语 from exam;

20、查询所有学生的分数,在显示的时候每门课加10分特长分。(每一门课程都加10分)

select id, name, chinese+10, math+10, english+10 from exam;

21、查询每个学生的总分。

select id, name, chinese+math+english as 总分 from exam;

22、对语文成绩升序排序后输出。

select * from exam order by chinese asc;

23、对语文升序排序,如果语文成绩一样,按数学成绩降序排序。

select * from exam order by chinese asc, math desc;

24、对总分排序按从高到低降序输出.

select *, chinese + math + english as 总分 from exam order by 总分 desc;

25、null值问题,显示所有学生的姓名和总成绩;如果求和的时候有一门分数为null,计算的结果也为null

select name, chinese+math+english as 总分 from exam;

26、查询显示学生姓名和总成绩。null的解决方案,ifnull(科目, 0)

Select name, ifnull(chinese,0) + ifnull(math,0) + ifnull(english,0) as 总分 from exam;

27、对姓刘的学生成绩总分进行降序排序

select *, ifnull(chinese,0) + ifnull(math, 0) + ifnull(english,0) as 总分
	from exam where name like '刘%'
		order by 总分 asc;

28、SQL中的 聚合 / 聚集 函数
在这里插入图片描述
29、count函数–统计记录数(统计行数)

// 注意: count在根据指定的列统计的时候,如果这一列中有null 不会被统计在其中。
select count(math) from exam;

30、统计一个班级共有多少学生?

select count(id) from exam;
select count(*) from exam;

31、统计语文成绩大于等于90的学生有多少个?

select * from exam where chinese >= 90;

32、统计总分大于250的人数有多少?

select *, ifnull(chiense,0) + ifnull(math,0) + ifnull(english,0) as 总分 from exam order by 总分 desc;
select count(*) from exam where ifnull(chinese,0) + ifnull(math,0) + ifnull(english,0) > 250;

33、统计一个班级数学总成绩?

// sum 可以排除 null.
select sum(math) from exam;

34、分别显示一个班级语文、英语、数学各科的总成绩

select sum(chinese), sum(math), sum(english) from exam;

35、统计一个班级语文、英语、数学的成绩总和。

select sum(chinese)+sum(math)+sum(english) as total from exam;
select sum(chinese + math + english) as total from exam; // 这个会出现null问题

36、统计一个班级语文成绩平均分

select sum(chinese) / count(*) from exam;

37、round(值, 小数) 函数

select round(sum(chinese) / count(*), 2) as 语文平均分 from exam;
发布了86 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43365369/article/details/93455367