以下操作均在MySQL5.7数据库上实验无误

需要四张表

Student_new(Sid,Sname,Sage,Ssex)学生表
Sid:学号
Sname:学生姓名
Sage:学生年龄
Ssex:学生性别 Course(Cid,Cname,Tid)课程表
Cid:课程编号
Cname:课程名称
Tid:教师编号 SC(Sid,Cid,score)成绩表
Sid:学号
Cid:课程编号
score:成绩 Teacher(Tid,Tname)教师表
Tid:教师编号:
Tname:教师名字

首先是建表与插入数据

CREATE  TABLE  Student_new (
Sid VARCHAR(20) NOT NULL UNIQUE PRIMARY KEY ,
Sname VARCHAR(20) NOT NULL ,
Sage datetime,
Ssex VARCHAR(20)
);
insert into Student_new values('' , '赵雷' , '1990-01-01' , '男');
insert into Student_new values('' , '钱电' , '1990-12-21' , '男');
insert into Student_new values('' , '孙风' , '1990-05-20' , '男');
insert into Student_new values('' , '李云' , '1990-08-06' , '男');
insert into Student_new values('' , '周梅' , '1991-12-01' , '女');
insert into Student_new values('' , '吴兰' , '1992-03-01' , '女');
insert into Student_new values('' , '郑竹' , '1989-07-01' , '女');
insert into Student_new values('' , '王菊' , '1990-01-20' , '女'); create table Course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into Course values('' , '语文' , '');
insert into Course values('' , '数学' , '');
insert into Course values('' , '英语' , ''); create table Teacher(Tid varchar(10),Tname varchar(10));
insert into Teacher values('' , '张三');
insert into Teacher values('' , '李四');
insert into Teacher values('' , '王五'); create table SC(Sid varchar(10),Cid varchar(10),score decimal(18,1));
insert into SC values('' , '' , 80);
insert into SC values('' , '' , 90);
insert into SC values('' , '' , 99);
insert into SC values('' , '' , 70);
insert into SC values('' , '' , 60);
insert into SC values('' , '' , 80);
insert into SC values('' , '' , 80);
insert into SC values('' , '' , 80);
insert into SC values('' , '' , 80);
insert into SC values('' , '' , 50);
insert into SC values('' , '' , 20);
insert into SC values('' , '' , 76);
insert into SC values('' , '' , 87);
insert into SC values('' , '' , 31);
insert into SC values('' , '' , 34);
insert into SC values('' , '' , 89);
insert into SC values('' , '' , 98);

问题如下:

1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

select student_new.Sid, student_new.Sname, student_new.Sage,student_new.Ssex, sc.score,sc.Cid
from student_new,sc where (student_new.Sid =
(select a.sid from
(select sid,score from sc where cid='') as a,
(select sid,score from sc where cid='') as b
where a.sid = b.sid and a.score>b.score)) and student_new.Sid=sc.Sid;

写得相当啰嗦,但思路很清楚,就是把student_new和sc两张表联合起来查询,大致框架已经有了,那就需要确定Sid学生编号,把课程编号是01和02的单独拿出来进行过滤得到符合要求的Sid

1.1 查询同时存在" 01 "课程和" 02 "课程的情况

select * from
(select * from sc where Cid='') as A, (select * from sc where Cid='') as B
where (A.Sid=B.Sid);

或者

select * from sc where (sc.Sid in
(select a.Sid from
(select * from sc where Cid='') as A, (select * from sc where Cid='') as B
where (A.Sid=B.Sid))) and sc.Cid in ('','');

注意这两种得到的结果不完全一样,第一个是把两个课程分数合在一行,第二个是两行

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

这个时候就要用到left join了

select * from (select * from SC where Cid='')A
left join (select * from SC where Cid='')B on A.Sid=B.Sid

其实1.1也可以用Left join 写,只不过B要加个B.Sid is not NULL的条件

left join 就是返回左表所有行(不满足条件就是NULL),然后返回右表满足条件的行(不满足就是NULL)

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

也有两种写法,第一种用left join,左表是存在02的情况,右表是存在01的情况,但是添加一个过滤条件,即右表为NULL,因为left join是左表都会列出来(只要满足on的匹配),右表为空则为NULL

select * from (select * from sc where Cid ='') as A left join (select * from sc where Cid='')B
on (A.Sid=B.Sid) where B.Cid is NULL

第二种,限定Sid不在01的行里面挑出Cid是02的行

select * from SC where Cid=''and Sid not in(select Sid from SC where Cid='')

2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

思路很清楚,把Sid和大于60的平均成绩拿出来作为一个表和学生表去连接

select A.Sid, student_new.Sname, A.avg_score from
(select Sid,AVG(score) as avg_score from sc group by Sid having AVG(score)>=60) as A, student_new
where (A.Sid = student_new.Sid);

3、查询在 SC 表存在成绩的学生信息

也很简单,用到一个select distinct,即去重选择

select * from student_new where Sid in (select distinct Sid from SC)

4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

同样用left join

select student_new.Sid,student_new.Sname,A.count,A.sum from student_new left join
(select Sid,count(Cid) as count,SUM(score) as sum from sc group by Sid) as A
on student_new.Sid = A.Sid;

4.1 查有成绩的学生信息

这个跟4就反了一下而已,只要把left join的左右表互换一下位置就好了,有成绩的学生信息一定能查到,或者用right join

select A.Sid,B.Sname,A.选课总数,A.总成绩 from
(select Sid,COUNT(Cid) as 选课总数,sum(score) as 总成绩 from sc group by Sid) as A
left join student_new as B on A.Sid=B.Sid

5. 查询「李」姓老师的数量

简单

select count(*) as 李姓老师数量 from teacher where Tname like '李%';

6. 查询学过「张三」老师授课的同学的信息

逻辑很清楚,三个表依次映射过去

select * from student_new where Sid in (select distinct Sid from sc where Cid in
(select Cid from course where Tid = (select Tid from teacher where Tname = '张三')))

7. 查询没有学全所有课程的同学的信息

select * from student_new where Sid in(
select Sid from sc group by Sid having count(Cid) <3
)

注意,HAVING要放到最后面

8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

想到了一种很朴素的方法,把每门课过滤掉‘01’,剩下其他同学,如果不为空,说明这门课是‘01’和其他人一起学的,把三门课的剩下学生加起来去重求并集就行了

select * from student_new where Sid in
(select Sid from SC where Cid='' and Sid <>''
UNION
select Sid from SC where Cid='' and Sid <>''
UNION
select Sid from SC where Cid='' and Sid <>'')

第二种方法,把‘01’学的三门课求出来,然后直接用去重的方式去选学这门课的学生

select * from student_new
where Sid in(select distinct Sid from sc where Cid in(select Cid from SC where Sid='')
) and Sid <> ''

9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

很屌的一种方法

select a.Sid,s.Sname from
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str
from sc where sid='')b,
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str
from sc group by sid)a
left join student_new s
on a.sid = s.sid
where a.cid_str = b.cid_str and a.Sid<>'';

但还是我自己写的好理解一点

select * from student_new where sid in(
select a.sid from
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str from sc where sid='') as b,
(select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str from sc group by sid) as a
where a.cid_str = b.cid_str and a.Sid<>'')

GROUP_CONCAT可以把一行里面的字符串组合起来,把‘01’的考试情况字符串和其他同学的考试情况字符串进行对比,找出一模一样的同学Sid就行了

10. 查询没学过"张三"老师讲授的任一门课程的学生姓名

select * from student_new where Sid not in (select distinct Sid from sc where Cid in
(select Cid from course where Tid = (select Tid from teacher where Tname = '张三')))

在第六题的基础上加个NOT就行了

未完待遇。。。

MySQL练习题2的更多相关文章

  1. MySQL练习题

    MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成 ...

  2. MySQL练习题参考答案

    MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...

  3. s15day12作业:MySQL练习题参考答案

    MySQL练习题参考答案   导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p ...

  4. Python/ MySQL练习题(一)

    Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...

  5. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  6. python 全栈开发,Day65(MySQL练习题,参考答案)

    一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...

  7. mysql 练习题答案

    一 题目 1.查询所有的课程的名称以及对应的任课老师姓名 2.查询学生表中男女生各有多少人 3.查询物理成绩等于100的学生的姓名 4.查询平均成绩大于八十分的同学的姓名和平均成绩 5.查询所有学生的 ...

  8. mysql练习题练习

    1.数据库是按照原文制作的,表格结构一样具体存储的数据有些差异 原文地址:MySQL练习题 原答案地址:MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: selec ...

  9. MySQL练习题及答案(复习)

    新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...

  10. mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风

    (-1)写在前面 文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-- ...

随机推荐

  1. CTR预估算法之FM, FFM, DeepFM及实践

    https://blog.csdn.net/john_xyz/article/details/78933253 目录目录CTR预估综述Factorization Machines(FM)算法原理代码实 ...

  2. C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】

    C#7.2——编写安全高效的C#代码 2018-11-07 18:59 by 沉睡的木木夕, 123 阅读, 0 评论, 收藏, 编辑 原文地址:https://docs.microsoft.com/ ...

  3. 偏离中轴的cos半球积分问题

    问题: 如果N与n重合,则就是普通的cos半球积分,地球人都知道结果是pi. 对于N与n不重合的一般情况,稍微麻烦一些. 解法1(同济高数课本的方法,参考同济高数第六版第二册“曲面积分”一章): 解法 ...

  4. FFmpeg: FFmepg中的sws_scale() 函数分析

    FFmpeg中的 sws_scale() 函数主要是用来做视频像素格式和分辨率的转换,其优势在于:可以在同一个函数里实现:1.图像色彩空间转换, 2:分辨率缩放,3:前后图像滤波处理.不足之处在于:效 ...

  5. Deep Reinforcement Learning

    Reinforcement-Learning-Introduction-Adaptive-Computation http://incompleteideas.net/book/bookdraft20 ...

  6. Troubleshooting Scheduler Autotask Issues (Doc ID 1561498.1)

    In this Document   Purpose   Troubleshooting Steps   References APPLIES TO: Oracle Database - Enterp ...

  7. 10款WordPress的插件让你的网站的移动体验

    随着科技的不断发展,需要改变营销策略的一个企业就变得非常重要.你不能指望用你的营销工具来留住你的客户.智能手机和平板电脑已经改变了消费者的行为方式.现在,人们甚至不想去他们的电脑或笔记本电脑,以检查产 ...

  8. C# 参数签名字符串按 ASCII码排序,注意其中的坑

    参数签名中通常是按键值对中键名称的ASCII按从小到大的顺序排序后进行hash为签名字符串.不要直接使用 SortedDictionary<string, string> 有坑的,他是按数 ...

  9. dockerd启动配置_修改IP和systemd管理

    docker采用CS架构,dockerd是管理后台进程,默认的配置文件为/etc/docker/daemon.json(--config-file可以指定非默认位置). 一个完整的daemon.jso ...

  10. 在Android中使用FFmpeg(android studio环境)

    1.首先我们需要一个已经编译好的libffmpeg.so文件.(怎么编译是个大坑,可以参考windows环境下编译android中使用的FFmpeg,也可以用网上下载的现成的,本文相关的github项 ...