继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

多表查询。。。。。。

不爱学习的gg坤
关注TA
已关注
手记 1
粉丝 1
获赞 18

三、多表查询:连接查询
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;

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP