SQL Server 查询选修了学分为3或4课程的学生名字(5种方法)

问题:查询选修了学分为3或4课程的学生名字

 

SQl 代码段: 

​
-- 查询选修了学分为3或4课程的学生名字
--自然连接
select distinct sname from 学生,选课,课程
where 学生.sid=选课.sid and 选课.cid=课程.cid
and (课程.credit=3 or 课程.credit=4)
--join on连接
select distinct sname from 学生 join 选课
on 学生.sid=选课.sid join 课程 on
选课.cid=课程.cid and (课程.credit=3 or 课程.credit=4)
--in 连接
select sname from 学生  where sid in(
select sid from 选课 where cid in (
select cid from 课程 where (credit=3 or credit=4)))
--exists 连接
select sname from 学生 where exists(
select * from 选课 where 学生.sid=选课.sid and exists(
select * from 课程 where 选课.cid=课程.cid))
--并集
select sname from 学生,选课,课程
where 学生.sid=选课.sid and 选课.cid=课程.cid and credit=3
union 
select sname from 学生,选课,课程
where 学生.sid=选课.sid and 选课.cid=课程.cid and credit=4

​

最后一个方法是求的查询两个表后的并集操作,同样也是可以得出结果的,总共采用了5种方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vous oublie@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值