当前位置: 移动技术网 > IT编程>开发语言>.net > MySQL中sql语句的应用

MySQL中sql语句的应用

2020年07月30日  | 移动技术网IT编程  | 我要评论
MySQL中sql语句的应用题目:(1)定义scor表和student表(2)在表中添加数据(3)查询student表的所有数据(4)查询student表的第2-4条记录(5)从student表中查询所有学生的学号(id)、姓名(name)和院系(department)的信息(6)从student表中查询计算机系和英语系的学生的信息(7)从student表中查询年龄18~22岁的学生信息(8)从student表中查询每个院系有多少人(9)从score表中查询每个科目的最高分(10)计算

MySQL中sql语句的应用

题目:

(1)定义scor表和student表
(2)在表中添加数据
(3)查询student表的所有数据
(4)查询student表的第2-4条记录
(5)从student表中查询所有学生的学号(id)、姓名(name)和院系(department)的信息
(6)从student表中查询计算机系和英语系的学生的信息
(7)从student表中查询年龄18~22岁的学生信息
(8)从student表中查询每个院系有多少人
(9)从score表中查询每个科目的最高分
(10)计算每个学生的总成绩
(11)计算每个考试科目的平均成绩
(12)查询计算机成绩低于95的学生信息
(13)将计算机考试成绩按从高到低进行排序|

其中,表的字段要求如下:

student:
student
score:
score

结果:

1、创建student和score表:

create table Student(
id int(10) primary key auto_increment,
name varchar(20) not null,
sex varchar(4),
birth datetime,
department varchar(20) not null,
address varchar(50)
)engine=InnoDB  default charset=utf8;

create table Score(
id int(10) primary key auto_increment not null unique,
stu_id int(10) not null,
c_name varchar(20),
grade int(10)
)engine=InnoDB  default charset=utf8;

2、插入数据

insert into Student(name, sex, birth, department, address) values('小明', '男', '1998-10-15 17:30:20', '材料学院', '成都金牛区');
insert into Student(name, sex, birth, department, address) values('小王', '男', '1998-10-15 17:30:20', '材料学院', '成都武侯区');
insert into Student(name, sex, birth, department, address) values('小张', '男', '1997-10-15 17:30:20', '数计学院', '重庆万州区');
insert into Student(name, sex, birth, department, address) values('小华', '男', '1997-10-15 17:30:20', '艺术学院', '重庆渝北区');
insert into Student(name, sex, birth, department, address) values('小花', '女', '1999-10-15 17:30:20', '人文学院', '成都青羊区');
insert into Student(name, sex, birth, department, address) values('小丁', '女', '2000-10-15 17:30:20', '外语学院', '广安枣山区');
insert into Student(name, sex, birth, department, address) values('小杨', '男', '1996-10-15 17:30:20', '机械学院', '重庆渝中区');

insert into Score(stu_id, c_name, grade) values (1, '思想政治', 90);
insert into Score(stu_id, c_name, grade) values (2, '思想政治', 80);
insert into Score(stu_id, c_name, grade) values (3, '思想政治', 70);
insert into Score(stu_id, c_name, grade) values (4, '思想政治', 77);
insert into Score(stu_id, c_name, grade) values (5, '思想政治', 85);
insert into Score(stu_id, c_name, grade) values (6, '思想政治', 79);
insert into Score(stu_id, c_name, grade) values (7, '思想政治', 82);
insert into Score(stu_id, c_name, grade) values (1, '大学英语1', 95);
insert into Score(stu_id, c_name, grade) values (2, '大学英语1', 90);
insert into Score(stu_id, c_name, grade) values (3, '大学英语1', 85);
insert into Score(stu_id, c_name, grade) values (4, '大学英语1', 83);
insert into Score(stu_id, c_name, grade) values (5, '大学英语1', 87);
insert into Score(stu_id, c_name, grade) values (6, '大学英语1', 91);
insert into Score(stu_id, c_name, grade) values (7, '大学英语1', 82);
insert into Score(stu_id, c_name, grade) values (1, '计算机应用技术', 95);
insert into Score(stu_id, c_name, grade) values (2, '计算机应用技术', 90);
insert into Score(stu_id, c_name, grade) values (3, '计算机应用技术', 85);
insert into Score(stu_id, c_name, grade) values (4, '计算机应用技术', 83);
insert into Score(stu_id, c_name, grade) values (5, '计算机应用技术', 87);
insert into Score(stu_id, c_name, grade) values (6, '计算机应用技术', 91);
insert into Score(stu_id, c_name, grade) values (7, '计算机应用技术', 82);

3、查询语句

#3查询student所有记录
select * from Student;
#4查询Student2-4条记录
select * from Student where id >= 2 and id <= 4;
#或者:
select * from Student where id between 2 and 4;
#5查询部分字段全部记录
select id,name,department from Student;
#6查询计算机和外语学院的学生
select * from Student where department = '数计学院' or department = '外语学院';
#7查询年龄在18-22的学生信息
select  * from Student where date_format(now(), '%Y') - date_format(birth, '%Y')  between 18 and 22;
#8查询每个学院的人数
select department, count(*) num from Student group by department;
#9查询每个科目最高分
select c_name, max(grade) maxgrade  from Score group by c_name;
#10计算每个学生的总成绩
select name, sum(grade) 总成绩 from Score, Student where Score.stu_id = Student.id group by stu_id;
#11计算每个考试科目的平均成绩
select c_name, avg(grade) avggrade from Score group by c_name;
#12查询计算机成绩低于95的学生
select name, sex, birth, department, address from Student, Score where Student.id = Score.stu_id and c_name = '计算机应用技术' and grade < 95;
#13将计算机考试成绩按从高到低排序
select * from Score where c_name = '计算机应用技术' order by grade desc;
纯属个人见解,如有错误,多谢指出!!!

本文地址:https://blog.csdn.net/qq_42602282/article/details/107642769

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网