MySQL查询之每日十题(一)MySQL查询练习结束:

MySQL查询练习

这是我参与8月更文挑战的第22天,活动详情查看:8月更文挑战

导读:

以下是MySQL中查询练习题,该练习题是个人整理的,如果哪些地方有错误或者疑问,欢迎指出;

个人使用navicate版本是15,mysql版本5.7.31

如果有些语句显示group by的问题,建议查看MySQL版本:

如果是mysql5.7.x版本,默认是开启了 only_full_group_by 模式,会导致代码报错;

解决方法:

1、查看sql_mode:

select @@global.sql_mode;
复制代码

查询出来的值为:

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
复制代码

2、去掉ONLY_FULL_GROUP_BY,重新设置值。

 set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
复制代码

3、上面是改变了全局sql_mode,对于新建的数据库有效。对于已存在的数据库,则需要在对应的数据下执行:

set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
复制代码

资源问题:

将student各表,以及study表放到百度网盘中,自取;

链接:pan.baidu.com/s/1CxZA_pb9…
提取码:1234

一、 实验目的

1、掌握查询语句的基本组成和使用方法。

2、掌握常用查询技巧。

二、 实验预习

1、 SQL中查询语句的语句格式:

Select 属性名 from 表名;
复制代码

2、 SQL中创建数据表的语句格式:

Create table 表名(

	字段名 字段类型,………

);
复制代码

三、 实验内容及要求

1、 数据库****db_student****中基本表的数据如下,输入下列数据。

学生表:Student

Sno Sname Ssex Sage Sdept
9512101 李勇 19 计算机系
9512103 王敏 20 计算机系
9521101 张莉 22 信息系
9521102 吴宾 21 信息系
9521103 张海 20 信息系
9531101 钱小平 18 数学系
9531102 王大力 19 数学系

课程表:Course

Cno Cname Ccredit Semster Period
C01 计算机导论 3 1 3
C02 VB 4 3 4
C03 计算机网络 4 7 4
C04 数据库基础 6 6 4
C05 高等数学 8 1 8

选课表:SC

Sno Cno Grade
9512101 C03 95
9512103 C03 51
9512101 C05 80
9512103 C05 NULL
9521101 C05 NULL
9521102 C05 80
9521103 C05 45
9531101 C05 81
9531101 C01 67
9531102 C05 94
9521103 C01 80
9512101 C01 NULL
9531102 C01 NULL
9512101 C02 87
9512101 C04 76

2、根据db_student中的数据,完成下列查询,将查询语句写在下方。

(1)查询全体学生的信息。

Select * from student;
复制代码

(2)查询“信息系”学生的学号,姓名和出生年份。

Select sno,sname,YEAR(NOW())-sage from student 

WHERE sdept='信息系';
复制代码

(3)查询考试不及格的学生的学号。

Select distinct sno from sc where grade<60;
复制代码

(4)查询无考试成绩的学生的学号和相应的课程号。

Select sno,cno from sc where grade is null;
复制代码

(5)将学生按年龄升序排序。

Select * from student order by sage;
复制代码

(6)查询选修了课程的学生的学号和姓名。

(要求:分别使用连接查询、嵌套子查询完成)

连接查询:

Select distinct student.sname,sc.sno from student,sc where student.sno=sc.sno ;
复制代码

嵌套子查询:

select sno,sname from student where sno in (
select distinct sno from sc);
复制代码

补充:

=any-->等于子查询结果中的某个值。

Select sno,sname from student where sno=any (select distinct sno from sc);
复制代码

(7)查询年龄在20-23岁之间的学生的系,姓名,年龄,按照系升序排序。

Select sname,sage,sdept from student where sage between 20 and 23 order by sdept;
复制代码

补充:

注意:utf8默认的校队集是utf-8-general-ci,他不是按照中文来的,需要强制让mysql按照中文来排序,gbk包含全部的中文字符,utf-8则包含全世界所有国家需要用到的字符;

Select sname,sage,sdept from student where sage between 20 and 23 order by convert(sdept using gbk);
复制代码

(8)查询选修了“计算机网络”或者“数据库基础”课程的学生的学号,姓名。

(要求:分别使用连接查询、嵌套子查询完成)

连接查询:

select distinct student.sno,sname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and (cname='计算机网络' or cname='数据库基础');
复制代码

嵌套查询:

select student.sno,sname 

from student

where sno in 

(

	select sno From sc

	where cno in (select cno from course where cname = '计算机网络' or cname = '数据库基础' ));
复制代码

补充:

联合查询:

Select sno,sname from student where sno in (select sno from course,sc where course.cno =sc.cno and cname ='计算机网络')
Union
Select sno,sname from student where sno in (select sno from course,sc where course.cno =sc.cno and cname ='数据库基础');
复制代码

(9)查询姓“张”的学生的基本信息。

select * from student where sname like'张%';
复制代码

(10)查询学生的选课情况,要求输出学号,姓名,课程门数,课程名列表(用逗号分隔),按照学号升序排序。

 SELECT student.sno,sname,COUNT(*),
 ​
 GROUP_CONCAT(cname ORDER BY cname SEPARATOR ',')'课程列表'
 ​
 FROM student,sc,course
 ​
 WHERE student.sno=sc.sno AND sc.cno=course.cno
 ​
 GROUP BY student.sno
 ​
 ORDER BY student.sno;
复制代码

结束:

如果你看到这里或者正好对你有所帮助,希望能点个👍或者⭐感谢;

有错误的地方,欢迎在评论指出,作者看到会进行修改。