数据sql 带有exists谓词的子查询

来源:百度知道 编辑:UC知道 时间:2024/05/30 17:38:50
学生S(S#,SNAME,AGE,SEX)
学习SC(S#,C#,GRADE)
课程C(C#,CNAME,TEACHER)。
要求:检索WANG同学未学的课程的课程号。
select c# from c
where not exists (
select * from s
where sname=wang and exists(
select * from sc
where sc.s#=s.s# and sc.c#=c.c#)) 这种写法为什么不能实现?应该怎么写?谢谢!
用exists

select C# from c where
not exists (select 1 from sc where sc.c#=c.c# and
exists (select 1 from s where s.s#=sc.s# and s.sname='WANG'))

select c# from c
where c# not in
(select c# from s,sc where sc.s#=s.s# and
s.sname='wang')

select C.* from C left join
(select * from SC inner join S on SC.S#=S.S# and S.sname='wang') A on C.C#=A.C# and A.C# is not null