mysql-sql-第九周

学习目标:

sql

学习内容:

25.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
分析:左连接

左连接两张表
Select s.name,c.name,sc.tscore from students s left join score sc on s.stunm=sc.stunm left join course c on sc.counm=c.counm;
在这里插入图片描述

显示所有
select *from students s left join score sc on s.stunm = sc.stunm left join course c on sc.counm = c.counm;

在这里插入图片描述

只连接一张表score,代码如下
select students.name, counm, tscore from students left join score on students.stunm = score.stunm;
在这里插入图片描述
26. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
分析:左连接

select students.name, tscore from students, score, course where students.stunm = score.stunm
and course.counm = score.counm
and course.name = “数学” and tscore < 60;

在这里插入图片描述
参考答案1
select s.name, tscore
from students s left join score sc on s.stunm = sc.stunm
left join course c on sc.counm = c.counm
where c.name = ‘数学’ and tscore < 60

参考答案2
select students.name, score.tscore from students, score, course where students.stunm = score.stunm
and course.counm = score.counm and course.name = “数学”
and score.tscore < 60;

27.查询同时存在"2001 “课程和” 2002 "课程的情况
分析:子查询
Select * from (select stunm ,tscore from score where counm=‘2001’) a ,(select stunm ,tscore from score where counm=‘2002’) b where a.stunm=b.stunm
在这里插入图片描述

28.查询存在" 2001 “课程但可能不存在” 2002 "课程的情况(不存在时显示为 null )
分析:左连接或者右连接
Select *from (select * from score where counm=‘2001’) a left join (select stunm,tscore b1 from score where counm=‘2002’) b on a.stunm=b.stunm
在这里插入图片描述

Select *from (select * from score where counm=‘2002’) a right join (select stunm,tscore b1 from score where counm=‘2001’) b on a.stunm=b.stunm
在这里插入图片描述

29.查询不存在"2001 “课程但存在” 2002 "课程的情况
Select*from score where score.stunm not in(select stunm from score where score.counm=‘2001’) and score.counm=‘2002’
分析:联查
在这里插入图片描述

30.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和
联合查询不会显示没选课的学生
分析:联查
Select score.stunm,name,sum(counm),sum(tscore) from score left join students on score.stunm=students.stunm group by score.stunm,name
在这里插入图片描述

学习时间:

1月-3月,每天一小时左右

学习产出:

一周一发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值