日期:2014-05-18  浏览次数:20644 次

紧急求助关于用到子查询的元组演算表达式(50分)
题目:
设学生选课关系模式为SC(Sno,Cno,Grade),其中Sno为学号,Cno为课程号,Grade为成绩.求跟学号050320197的学生选择相同课程的所有学生的学号

写出元组演算表达式?

------解决方案--------------------
SQL server的语句是这么写的

select distinct sno from SC a
where not exists (
select 1 from SC b
where sno = a.sNo
and not exists (
select 1 from SC
where cno=b.cNo
and sno = '050320197 '
)
)


------解决方案--------------------
用楼主的表:
if object_id( 'sc ') is not null
drop table sc
GO
create table sc(sno int , cno int, grade int)
insert into sc values(1,01,11)
insert into sc values(1,02,12)
insert into sc values(2,01,11)
insert into sc values(2,02,11)
insert into sc values(3,01,11)

declare @Sno int
set @Sno = 1
SELECT Sno FROM sc as t
WHERE NOT EXISTS(
select 1 from sc as a left join (select * from sc where Sno =@Sno) as b
on a.Cno = b.Cno
where a.Sno = t.Sno AND b.Sno IS NULL)
GROUP BY Sno
HAVING COUNT(Sno) = (select count(Cno) from sc where Sno =@Sno)


/*结果
Sno
----------
1
2
*/