MySQL操作示例
"""
MySQL综合练习作业
""" # 1、自行创建测试数据;
# 创建数据库
"""
create database school;
use school;
"""
# 创建表结构
"""
create table class(
cid int primary key auto_increment,
caption char(10) not null,
grade_id int unsigned not null
)engine=InnoDB default charset=utf8; # 班级表 create table student(
sid int primary key auto_increment,
sname char(10) not null,
gender enum("male","female") not null default "male",
class_id int unsigned not null
)engine=InnoDB default charset=utf8; # 学生表 create table teacher(
tid int primary key auto_increment,
tname char(10) not null
)engine=InnoDB default charset=utf8; # 教师表 create table course(
cid int primary key auto_increment,
cname char(10) not null,
teacher_id int unsigned not null
)engine=InnoDB default charset=utf8; # 课程表 create table score(
sid int primary key auto_increment,
student_id int unsigned not null,
course_id int unsigned not null,
score int unsigned not null
)engine=InnoDB default charset=utf8; # 课程表 create table class_grade(
gid int primary key auto_increment,
gname char(20) not null
)engine=InnoDB default charset=utf8; # 年级表 create table teach2cls(
tcid int primary key auto_increment,
tid int unsigned not null,
cid int unsigned not null
)engine=InnoDB default charset=utf8; # 班级任职表 """
# 插入记录
"""
insert into class(caption,grade_id) values
("一年一班",1),
("二年一班",2),
("一年二班",1),
("二年二班",2),
("二年三班",2),
("三年一班",3),
("四年一班",4),
("四年二班",4); insert into student(sname,gender,class_id) values
("judy", "female", 1),
("张三", "male", 1),
("李四", "female", 1),
("李武", "male", 1),
("kitty", "female", 2),
("walter", "female", 2),
("doris", "male", 2),
("周公", "male", 2),
("胡图", "male", 2),
("李毅", "male", 2),
("李二", "female", 3),
("李三", "female", 3),
("李柳", "male", 3),
("张八", "male", 3),
("王二", "male", 3),
("王三", "male", 3),
("王八", "female", 3),
("张八", "male", 4),
("王二", "female", 4),
("王三", "male", 4); insert into teacher(tname) values
("王五"),
("李平"),
("李艳"); insert into course(cname,teacher_id) values
("生物", 1),
("物理", 2),
("化学", 2),
("英语", 3),
("语文", 2); insert into score(student_id, course_id,score) values
(1,1,80),
(1,2,90),
(1,3,88),
(1,4,100),
(1,5,100),
(2,1,67),
(2,3,99),
(3,1,55),
(3,2,33),
(3,4,80),
(4,2,56),
(4,3,23),
(4,4,88),
(5,1,78),
(6,2,45),
(6,4,100),
(6,3,80),
(7,1,98),
(7,2,85),
(7,3,87),
(7,4,74),
(8,1,81),
(8,2,82),
(8,3,90),
(9,4,96),
(10,1,88),
(12,1,80); insert into class_grade(gname) values
("一年级"),
("二年级"),
("三年级"),
("四年级"),
("五年级"); insert into teach2cls(tid,cid) values
(1,1),
(1,2),
(2,2),
(3,1),
(3,2),
(3,3);
""" # 2、查询学生总人数;
"""
select count(sid) as count_stu from student;
""" # 3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
"""
select sid,sname from student inner join
(select t1.student_id,t1.biology_score,t2.physical_score from
(select student_id,score as biology_score from score where course_id =
(select cid from course where cname = "生物") ) as t1 inner join
(select student_id,score as physical_score from score where course_id =
(select cid from course where cname = "物理")) as t2
on t1.student_id = t2.student_id
having biology_score >= 60 and physical_score >= 60) as t3
on t3.student_id = student.sid;
""" # 4、查询每个年级的班级数,取出班级数最多的前三个年级;
"""
select gname,t1.class_count from class_grade inner join
(select grade_id,count(cid) as class_count from class
group by grade_id
order by class_count desc
limit 3) as t1
on gid = t1.grade_id;
""" # 5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
"""
select sid,sname,avg_score from student inner join
((select student_id,avg(score) as avg_score from score
group by student_id
order by avg_score desc
limit 1)
union
(select student_id,avg(score) as avg_score from score
group by student_id
order by avg_score asc
limit 1)) as t1
on sid = t1.student_id; """ # 6、查询每个年级的学生人数;
"""
select gname,stu_num from class_grade inner join
(select cid,t1.stu_num,grade_id from class inner join
(select class_id,count(sid) as stu_num from student
group by class_id) as t1
on cid = class_id) as t2
on gid = grade_id;
""" # 7、查询每位学生的学号,姓名,选课数,平均成绩;
"""
select sid,sname,course_num,avg_score from student inner join
(select student_id,count(course_id) as course_num,avg(score) as avg_score from score
group by student_id) as t1
on sid = student_id;
""" # 8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
"""
select sname,student_id,cname,score from student innr join
(select student_id,cname,score from course inner join
((select * from score where student_id = 2
order by score desc
limit 1)
union
(select * from score where student_id = 2
order by score asc
limit 1)) as t1
on course_id = cid) as t2
on sid = student_id; """ # 9、查询姓“李”的老师的个数和所带班级数;
"""
select count(distinct t2.cid) as class_count,count(distinct t2.tid) as teach_class_count from
(select teach2cls.tid,teach2cls.cid from teach2cls inner join
(select tid from teacher where tname like "李%") as t1
on t1.tid = teach2cls.tid) as t2;
""" # 10、查询班级数小于5的年级id和年级名;
"""
select gid,gname from class_grade inner join
(select grade_id,count(cid),group_concat(caption) from class
group by grade_id
having count(cid) < 5) as t1
on gid = grade_id;
""" # 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
"""
select cid,caption,gname,
case
when gname in ("一年级","二年级") then "低年级"
when gname in ("三年级","四年级") then "中年级"
when gname in ("五年级","六年级") then "高年级"
end as "grade_level"
from class inner join
class_grade
on grade_id = gid;
""" # 12、查询学过“李平”老师2门课以上的同学的学号、姓名;
"""
select sid,sname from student where sid in
(select student_id from score where course_id in
(select cid from course where teacher_id = (select tid from teacher where tname = "李平"))
group by student_id
having count(course_id) > 2);
""" # 13、查询教授课程超过2门的老师的id和姓名;
"""
select tid,tname from teacher where tid in
(select teacher_id from course
group by teacher_id
having count(cid) > 2);
""" # 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
"""
select sid,sname from student inner join
(select distinct student_id from score where course_id in (1,2)) as t1
on student_id = sid;
""" # 15、查询没有带过高年级的老师id和姓名;
"""
select teacher.tid,tname from teacher inner join
(select distinct tid from teach2cls where cid not in (5,6))as t1
on teacher.tid = t1.tid;
""" # 16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
""" select sid,sname from student where sid in
(select student_id from score,course,teacher
where score.course_id = course.cid and course.teacher_id = teacher.tid and teacher.tname = "李平"
group by student_id
having count(course_id) = (select count(cid) from course,teacher
where course.teacher_id = teacher.tid and teacher.tname = "李平")); """ # 17、查询带过超过2个班级的老师的id和姓名;
"""
select teacher.tid,tname from teacher inner join (
select tid, count(cid) from teach2cls
group by tid
having count(cid) > 2) as t1
on teacher.tid = t1.tid;
""" # 18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
"""
select sid,sname from student where sid in
(select t3.student_id from
(select t1.student_id,t1.score2,t2.score1 from
(select student_id,score as score2 from score where course_id = 2) as t1 inner join
(select student_id,score as score1 from score where course_id = 1) as t2
on t1.student_id = t2.student_id) as t3
where t3.score2 < t3.score1
);
""" # 19、查询所带班级数最多的老师id和姓名;
"""
select tid,tname from teacher where tid =
(select tid from teach2cls
group by tid
order by count(cid) desc
limit 1
); """ # 20、查询有课程成绩小于60分的同学的学号、姓名;
"""
select sid,sname from student
where sid in
(select distinct student_id from score
where score < 60
); """ # 21、查询没有学全所有课的同学的学号、姓名;
"""
select sid,sname from student where sid not in
(select student_id from score
group by student_id
having count(course_id) = (select count(cid) from course)
);
""" # 22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
"""
select sid,sname from student where sid in (
select student_id from score
where course_id in
(select course_id from score where student_id = 1)
);
""" # 23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
"""
select sid,sname from student where sid in (
select student_id from score
where student_id != 1 and course_id in
(select course_id from score where student_id = 1)
);
""" # 24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
"""
select sid,sname from student where sid in (
select student_id from score
where student_id != 2
group by student_id
having group_concat(course_id) =
(select group_concat(course_id) from score
where student_id = 2
group by student_id
)
);
""" # 25、删除学习“王五”老师课的score表记录;
"""
delete from score where course_id in (
select cid from course where teacher_id = (
select tid from teacher where tname = "王五"
));
""" # 26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
"""
insert into score(student_id,course_id,score)
select t1.sid,t1.cid,t2.avg_score from(
(select sid,2 as cid from student where sid not in (
select student_id from score where course_id = 2)) as t1,
(select avg(score) as avg_score from score where course_id = 2) as t2
);
""" # 27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示:
# 学生ID,语文,数学,英语,有效课程数,有效平均分;
""" select t1.student_id,
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "语文")) as "语文",
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "数学")) as "数学",
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "英语")) as "英语",
count(t1.course_id) as "有效课程数",avg(t1.score) as "有效平均分"
from score as t1 where course_id in (select cid from course where cname in ("语文","数学","英语"))
group by t1.student_id
order by avg(t1.score) asc; """ # 28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
"""
select course.cid,t1.max_sc,t1.min_sc from course
left join
(select course_id,max(score.score) as max_sc,min(score.score) as min_sc from score
group by course_id) as t1
on course.cid = t1.course_id;
""" # 29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
"""
select course_id,avg(score) as avg_sc,
concat(sum(case when score >= 60 then 1 else 0 end)/count(*) * 100, "%")as percent
from score
group by course_id
order by avg_sc asc, percent desc; """ # 30、课程平均分从高到低显示(现实任课老师);
"""
select cid,cname,avg_sc,tname from
(select cid,cname,tname from course inner join teacher
on teacher_id = tid) as t1
left join
(select course_id,avg(score) as avg_sc from score
group by course_id )as t2
on cid = course_id
order by avg_sc desc;
""" # 31、查询各科成绩前三名的记录(不考虑成绩并列情况)
"""
select course.cid,course.cname,t3.first_sc,t3.second_sc,t3.third_sc from
(select course_id,
(select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 0, 1) as first_sc,
(select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 1, 1) as second_sc,
(select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 2, 1) as third_sc
from score as t1
group by course_id) as t3
right join course
on course.cid = t3.course_id; 方法二:
SELECT
student_id,
score,
course_id
FROM score r1
WHERE (SELECT count(1)
FROM (SELECT DISTINCT
score,
course_id
FROM score) r2
WHERE r2.course_id = r1.course_id AND r2.score > r1.score) <= 2
ORDER BY course_id, score DESC;
""" # 32、查询每门课程被选修的学生数;
"""
select course_id,count(student_id) as stu_num from score
group by course_id; """ # 33、查询选修了2门以上课程的全部学生的学号和姓名;
"""
select sid,sname from student where sid in (
select student_id from score
group by student_id
having count(course_id) > 2
);
""" # 34、查询男生、女生的人数,按倒序排列;
"""
select gender,count(sid) as stu_num from student
group by gender
order by stu_num asc;
""" # 35、查询姓“张”的学生名单;
"""
select * from student where sname like "张%";
""" # 36、查询同名同姓学生名单,并统计同名人数;
"""
select sname,group_concat(sid) as sid,count(sid) as same_num from student
group by sname
having count(sid) >= 2;
""" # 37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
"""
select course_id,avg(score) as avg_score from score
group by course_id
order by avg(score) asc, course_id desc;
""" # 38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;物理
"""
select sname,score from student inner join
(select student_id,score from score where score < 60 and course_id = (select cid from course where cname = "数学"))
as t1
on student.sid = t1.student_id; """ # 39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
"""
select sid,sname from student
inner join (
select student_id,score from score where course_id = 3
having score > 80
)as t1
on sid = student_id; """ # 40、求选修了课程的学生人数
"""
select count(t1.student_id) from(
select student_id from score
group by student_id) as t1;
""" # 41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
"""
select sname,score from student
inner join
((select student_id,score from
(select student_id,score,course_id from score as t1
where course_id in (
select cid from course
where teacher_id = (select tid from teacher where tname = "李平"))) as t1
order by score desc
limit 1)
union
(select student_id,score from
(select student_id,score,course_id from score as t1
where course_id in (
select cid from course
where teacher_id = (select tid from teacher where tname = "李平"))) as t1
order by score asc
limit 1)) as t2
on sid = student_id; """ # 42、查询各个课程及相应的选修人数;
"""
select cname,t1.stu_num from course
inner join
(select course_id,count(student_id) as stu_num from score
group by course_id) as t1
on course.cid = t1.course_id;
""" # 43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
"""
select t1.student_id,t1.course_id,t1.score from score as t1, score as t2
where t1.student_id != t2.student_id and t1.course_id != t2.course_id and t1.score = t2.score
order by t1.score desc;
""" # 44、查询每门课程成绩最好的前两名学生id和姓名;
"""
select student.sid,student.sname,t4.course_id,t4.score from
(select score.student_id,score.course_id,score.score from
(select t1.course_id,
(select score from score as t2 where t1.course_id = t2.course_id order by t2.score desc limit 0,1) as first_sc,
(select score from score as t2 where t1.course_id = t2.course_id order by t2.score desc limit 1,1) as second_sc
from score as t1
group by t1.course_id) as t3
inner join score
on score.course_id = t3.course_id
where score.score in (t3.first_sc, t3.second_sc)) as t4
inner join student
on student.sid = t4.student_id
order by t4.course_id desc;
""" # 45、检索至少选修两门课程的学生学号;
"""
select student_id from score
group by student_id
having count(course_id) > 1;
""" # 46、查询没有学生选修的课程的课程号和课程名;
"""
select cid,cname from course where cid not in(
select course_id from score
group by course_id); """ # 47、查询没带过任何班级的老师id和姓名;
"""
insert into teacher(tname) values ("张**"); select tid,tname from teacher where tid not in (
select tid from teach2cls
group by tid
);
""" # 48、查询有两门以上课程超过80分的学生id及其平均成绩;
"""
select student_id,avg(score) as avg_sc from score
where score > 80
group by student_id
having count(course_id) > 2;
""" # 49、检索“3”课程分数小于60,按分数降序排列的同学学号;
"""
select student_id from score
where course_id = 3 and score < 60
order by score desc; """ # 50、删除编号为“2”的同学的“1”课程的成绩;
"""
insert into score(student_id,course_id,score) values (2,1,88); delete from score where student_id = 2 and course_id = 1;
""" # 51、查询同时选修了物理课和生物课的学生id和姓名;
"""
select sid,sname from student where sid in (
select t1.student_id from score as t1
where t1.course_id in (select cid from course where cname in ("物理","生物"))
group by t1.student_id
having count(t1.course_id) = 2);
"""
MySQL操作示例的更多相关文章
- PHP中MySQL操作
本次使用的demo是MySQL的示例数据库employees,点击下载地址,注意在导入的时候,在employees.sql文件中,将source改成你当前的目录. PHP中的demo代码可以在ideo ...
- Mysql操作初级
Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...
- 第一篇:Mysql操作初级
Mysql操作初级 Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如: ...
- python开发学习-day09(队列、多路IO阻塞、堡垒机模块、mysql操作模块)
s12-20160312-day09 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Mysql语句示例
Mysql语句示例 最常用 sql 语句总结 前言 Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句 ...
- phpExcel 操作示例
片段 1 片段 2 phpExcel 操作示例 <?php //写excel //Include class require_once('Classes/PHPExcel.php'); requ ...
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- 学习笔记:MySQL操作初步
对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...
- ecshop的Mysql操作类
摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...
随机推荐
- Taro 采坑日常
组件事件传参只能在类作用域下的确切引用(this.handleXX || this.props.handleXX),或使用 bind. 组件中点击事件如下 // 组件 <AtListItem k ...
- Logstash读取文本信息并写入到ES
Logstash读取文本信息并写入到ES 前提是ELK安装没问题 进入到logstash安装目录下的bin目录(我的logstash安装目录:/usr/local/) [root@es1 bin]# ...
- java启动参数二
非标准参数又称为扩展参数,其列表如下: -Xint 设置jvm以解释模式运行,所有的字节码将被直接执行,而不会编译成本地码. -Xbatch 关闭后台代码编译,强制在前台编译,编译完成之后才能进行代码 ...
- NOIP-2018
时隔一年,再度踏入NOIp的考场,内心感慨万分 Day0 在中巴上昏睡了3h++,终于到了长沙理工大学,国际学术交流中心......不太对,好像是国际交流中心与综合实验楼连线--理工大学的另一个大门外 ...
- 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
- 在Eclipse+ADT中开发Android系统的内置应用
转自: http://www.iteye.com/topic/1050439 在Eclipse+ADT中开发Android系统的内置应用 Android系统内置有:Browser(浏览器).Mms( ...
- Android开发学习——android反编译
第一种: 1.下载下边的三个jar包,对dex2jar和jd-gui进行解压 2.将需要反编译的apk后缀名改为.rar,然后进行解压 3.将解压后生成的classes.dex加入第一步解压后的dex ...
- 策略模式--Java篇
策略模式(Strategy):它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 下面将以商场打折为例子,说明策略模式.商场收银如何促销,用打折还是 ...
- mvc的生命周期
尽管每次都是从msdn里复制粘贴,但是还是有必要的,加深对mvc的理解和官方的表述. 了解 MVC 应用程序执行过程 发送给基于 ASP.NET MVC 的 Web 应用程序的请求首先通过 UrlRo ...
- 掌握Spark机器学习库-06-基础统计部分
说明 本章主要讲解基础统计部分,包括基本统计.假设检验.相关系数等 数据集 数据集有两个文件,分别是: beijing.txt 北京历年降水量,不带年份 beijing2.txt 北京历年降水量,带年 ...