本文案例导入以下sql文本即可,通过百度网盘下载
mysql> source h:\db_school.sql
网盘链接:https://pan.baidu.com/s/1rvJhB6it8rvOMeqMjRAGlg?pwd=0hx1
1.表结构及信息
1.1 tb_class
1.2 tb_course
1.3 tb_course
1.3 tb_course
2.实战案例
1.查询所有班级的班级编号、所属学院和班级名称
select classNo,department,className from tb_class;
2.从tb_class表中查询所有的学院名称。
select distinct department from tb_class;
3.查询全体学生的详细信息
select * from tb_student;
4.查询全体学生的姓名、性别和年龄
select studentName,sex,year(now())-year(birthday) age from tb_student;
5.查询全体学生的姓名、性别和年龄,要求用汉语显示目标列表的名称。
select studentName as ‘姓名’,
sex as ‘性别’,
year(now())-year(birthday) as ‘年龄’
from tb_student;
6.查询课时大于等于48学时的课程名称和学分
select courseName,credit from tb_course
where courseHour >= 48;
7.查询少数民族学生的姓名、性别、籍贯和民族
select studentName,sex,nation, native
from tb_student where nation <> ‘汉’;
8.查询1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student
where year(birthday)=1997;
9.查询不是1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student where year(birthday)!=1997;
10.查询籍贯是北京、天津和上海的学生信息。
select * from tb_student
where native in (‘北京’,‘天津’,‘上海’);
11.查询籍贯不是北京、天津和上海的学生信息。
select * from tb_student
where native not in (‘北京’,‘天津’,‘上海’);
12.查询2013年入学的学生全部信息。
select * from tb_student
where left(studentNo,4)=‘2013’;
13.查询所有姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like ‘王%’;
14.查询所有不姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName not like ‘王%’;
15.查询姓名中包含‘林’字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like ‘%林%’;
16.查询姓王的且姓名为三个字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName like ‘王%’ and CHAR_LENGTH(studentName)=3;
17.查询课程名称中包含’-‘符号的课程信息;
select * from tb_course where courseName like ‘%-%’;
18.查询课程名称中带有中文‘系统’的课程信息。
select * from tb_course where courseName like ‘%系统%’;
19.查询课程名称中含有‘管理’、‘信息’或者‘系统’的课程信息。
select * from tb_course
where courseName like ‘%管理%’ or courseName like ‘%信息%’ or courseName like ‘%系统%’;
20.查询缺少先修课的课程信息。
select * from tb_course where priorCourse is null;
21.查询所有有先修课的课程信息
select * from tb_course where priorCourse is not null;
22.查询学分大于等于3且学时数大于32的的课程名称、学分和学时数。
select courseName,credit,courseHour
from tb_course
where credit=3 and courseHour>32;
23.查询籍贯是北京或者上海的学生的姓名、籍贯和民族。
select studentName,native, nation
from tb_student
where native in (‘北京’,‘上海’);
24.查询籍贯是北京或湖南的少数民族男生的姓名、籍贯和民族。
select studentName,native, nation
from tb_student where native in (‘北京’,‘湖南’)
and nation<>‘汉’;
25.查询学生的姓名、籍贯和民族,并将查询结果按姓名升序排序。
select * from tb_student order by studentName;
26.查询学生选课成绩大于85分的学号、课程号和成绩信息,
并将查询结果先按学号升序排列,再按成绩降序排列。
select * from tb_score
where score>85
order by studentNo,score desc;
27.查询成绩排名第3至第5的学生学号、课程号和成绩
select * from tb_score
order by score desc limit 2,3;