为了账号安全,请及时绑定邮箱和手机立即绑定

多表查询。。。。。。

标签:
MySQL

三、多表查询:连接查询
select tb_student.sname, tb_student.sage,tb_score.sclassname, tb_score.sscore
from tb_student, tb_score
where tb_student.sno = tb_score.sno
=> 精简代码,给表取别名
select a.sname, a.sage,b.sclassname, b.sscore
from tb_student a, tb_score b
where a.sno = b.sno
三表等值连接:
查询学生的成绩表,要求显示学生学号,姓名,性别,课程名和成绩
学号,姓名,性别 来源于 tb_student
课程名 —— tb_class
成绩 ——— tb_score
select a.sno, a.sname,a.ssex,c.cname,b.sscore
from tb_student a, tb_score b, tb_class c
where a.sno = b.sno
and b.cno = c.cno;
四、子查询:
在where子句中,值使用一个查询语句替代
请查询考试成绩大于70分的学生,显示学生编号,姓名和专业
分析:
1、考试成绩大于70分有哪些学生,数据来源于成绩表
可以查出符合条件的学生编号
2、根据第一步的结果,再查询学生表的信息
1、查询成绩大于70分的学号
select distinct sno
from tb_score
where sscore > 70
2、根据学号列表,找姓名和专业
--子查询实现
select sno, sname, sdept
from tb_student
where sno IN(
select distinct sno
from tb_score
where sscore > 70
)
也可以用连接查询实现:
select distinct a.sno, a.sname, a.sdept
from tb_student a, tb_score b
where a.sno = b.sno
and b.sscore > 70;

点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消