注册 登录
编程论坛 SQL Server论坛

一个查询问题,作为一个初学者,苦思冥想无法解答,望有时间的人能指点指点!

wuyigong 发布于 2010-08-06 00:59, 1234 次点击
查询学过学号为“001”同学所有课程的其他同学学号?

成绩表SC(学号 Sid,课程号Cid,分数 Score)
14 回复
#2
tk_unico2010-08-06 05:25
select sid from sc where cid in
(select cid from sc where sid='001') and sid<>'001'


看看是不是这样
#3
cnfarer2010-08-06 06:46
试试吧
select sid from sc a where not exists (SELECT * FROM sc WHERE sc.sid=a.sid and sc.cid NOT in (SELECT cid FROM sc WHERE sc.sid='001'))
#4
dearwolf41282010-08-06 09:07
回复 2楼 tk_unico
他的是正确的
#5
dearwolf41282010-08-06 09:09
二楼的是正确的,3楼语句有点复杂
#6
cnfarer2010-08-07 19:43
不试是出不来结果的,3楼的缺点是没有排除001
#7
wuyigong2010-08-07 22:18
回复 楼主 wuyigong
试了,不行啊。。 估计你是忽略了题目中的“所有”2个字。
#8
wuyigong2010-08-07 22:19
回复 2楼 tk_unico
试了,不行啊。。 估计你是忽略了题目中的“所有”2个字。
#9
wuyigong2010-08-07 22:19
回复 2楼 tk_unico
试了,不行啊。。 估计你是忽略了题目中的“所有”2个字。
#10
SQLCenter2010-08-08 04:08
程序代码:
--> 学过001同学所有课程的其他同学
select a.Sid from SC a inner join
    (select Cid from SC where Sid = '001') b on a.Cid = b.Cid where a.Sid <> '001'
group by a.Sid
    having count([color=#800000; font-weight: bold]1[/color]) = (select count([color=#800000; font-weight: bold]1[/color]) from SC where Sid = '001')

--> 与001同学所学课程完全一样的:inner join 改为 left join

#11
SQLCenter2010-08-08 04:10
/*程序代码显示有问题*/

--> 学过001同学所有课程的其他同学
select a.Sid from SC a inner join
    (select Cid from SC where Sid = '001') b on a.Cid = b.Cid where a.Sid <> '001'
group by a.Sid
    having count(1) = (select count(1) from SC where Sid = '001')

--> 与001同学所学课程完全一样的:inner join 改为 left join
#12
wuyigong2010-08-08 12:44
回复 10楼 SQLCenter
count [color=#800000; font-weight: bold]1[/color]) = (select count([color=#800000; font-weight: bold]1[/color]

上面一段有点看不懂哦

但是你的程序估计有一点小小的问题啦,以下是报错:
消息 102,级别 15,状态 1,第 4 行
'1' 附近有语法错误。
消息 102,级别 15,状态 1,第 4 行
'1' 附近有语法错误。
#13
wuyigong2010-08-08 12:46
回复 11楼 SQLCenter
哦,知道了,出来了,谢了呵呵
#14
aei1352010-08-09 11:16
select distinct sid from sc a where not exists(
select cid from sc b where sid='001' and not exists(select cid from sc c where sc.sid=a.sid and sc.cid=b.cid))
and sid<>'001'
#15
yakecan19882010-08-09 12:43
要用到嵌套吧~
1