查询和XXX同学学习的课程完全相同的其他同学学号和姓名

create table score(

s_id varchar(20),
c_id varchar(20),
s_score int(3),
primary key(s_id,c_id)
);
create table student(
s_id varchar(20),
s_name varchar(20) not null,
primary key(s_id)
);

第一步:查询XXX同学选的所有课

select c_id
from score
where s_id='xxx'

第二步:not in找出选了其他课(xxx同学没选的课)的同学

select s_id
from score stemp
where c_id not in (
select c_id
from score
where s_id='xxx')

第三步:选出不在上一步选出的同学中是同学



select s_id
from score temp
where s_id not in(
select s_id
from score stemp
where c_id not in (
select c_id
from score
where s_id='xxx') )

第四步:对这些同学group by后判断选课数量是否和xxx同学选课数量一样 

select s_id,s_name
from student
where s_id in(
select s_id
from score temp
where s_id not in(
select s_id
from score 
where c_id not in (
select c_id
from score
where s_id='xxx') )
group by s_id
having count(*)=(select count(*) from score where s_id ='xxx')) and s_id!='xxx';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值