热线电话:13121318867

登录
2020-04-09 阅读量: 788
MySQL:学生信息查询(难题)

问题:查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

表结构:

create table sc(

Sid int,

Cid int,

score decimal(18,1));

-- 成绩表 SC 录入信息

insert into sc values

('01' , '01' , 80),

('01' , '02' , 90),

('01' , '03' , 99),

('02' , '01' , 70),

('02' , '02' , 60),

('02' , '03' , 80),

('03' , '01' , 80),

('03' , '02' , 80),

('03' , '03' , 80),

('04' , '01' , 50),

('04' , '02' , 30),

('04' , '03' , 20),

('05' , '01' , 76),

('05' , '02' , 87),

('06' , '01' , 31),

('06' , '03' , 34),

('07' , '02' , 89),

('07' , '03' , 98);

解题思路及答案:

select *

from student

where student.SId not in (

select t1.SId

from

(select student.SId,t.CId

from student ,(select sc.CId from sc where sc.SId='01') as t )as t1

left join sc on t1.SId=sc.SId and t1.CId=sc.CId

where sc.CId is null )

and student.SId !='01';

# 第1步:查询出01学生所选课程作为表t

# 第2步:用笛卡尔积合并student表和t表作为t1表

# 第3步:用t1表左连sc表,且用复合主键(sid,cid)关联,筛选出和01学生选课不同的学生

# 第4步:从student表中将与01学生选课不同的学生排除

16.6668
5
关注作者
收藏
评论(0)

发表评论

暂无数据