MySQL三表查询(学生表、课程表、成绩表)查询出语文成绩比数学成绩高的学生信息

有三张表

学生表

课程表

成绩表

要求查处语文成绩低于数学成绩的学生信息

先去课程表中查出课程cid

select cid from course where cname='语文';
select cid from course where cname='数学';

然后课程cid作为查询条件去成绩表中查处所有语文成绩 作为一个结果集tempChinese 数学成绩作为一个结果集tempMath

select * from score where corse_id=(
 select cid from course where cname='语文');

select * from score where corse_id=(
 select cid from course where cname='数学');

语文成绩的结果集tempChinese 

tempMath

此时再用student tempChinese tempMath 做关联查询

select student.sid,student.sname
from student ,
(select * from score where corse_id=(
 select cid from course where cname='语文'))as tempChinese,
(select * from score where corse_id=(
 select cid from course where cname='数学'))as tempMath
where student.sid=tempChinese.sid
and tempChinese.sid = tempMath.sid
and tempChinese.number<tempMath.number;

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值