求出与学生02学的课程一模一样的同学编号

shidili2008 2012-05-30 02:57:44

学生表s:s#,tname
课程表c:c#,cname
成绩表sc:s#,c#,grade
通过这3张表如何查出哪些学生与特定编号的学生(比如02)学过一模一样的课程呢?
比如:

sc表中如下数据:


s# c# grade
01 1 80
01 2 90

02 1 80
02 2 80

03 1 79
03 2 80
03 3 90
结果应该是学生01与02学过一样课程,求哪位兄弟写个sql语句呢。。。。。万分感谢
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shidili 2012-05-30
  • 打赏
  • 举报
回复
一个用户不能连续回复3次以上,所以换了个号。。。


哎,,,,可能我题目说的有些问题,我是说跟02学生学的是一模一样的课程,不能多也不能少,所以03,04我并不需要。。。
shidili2008 2012-05-30
  • 打赏
  • 举报
回复
magician547兄弟是把05也查出来了,TravyLee大哥是把03,04也查出来了呢。。。。。
shidili2008 2012-05-30
  • 打赏
  • 举报
回复
查询结果应该为01 说错了
shidili2008 2012-05-30
  • 打赏
  • 举报
回复
上位2位哥们,好像你们的答案都有些问题呢。。。
我把数据多添加了几条如下:
insert [test]
select '01',1,80 union all
select '01',2,90 union all
select '02',1,80 union all
select '02',2,80 union all

select '03',1,79 union all
select '03',2,80 union all
select '03',3,90 union all
select '04',1,90 union all
select '04',2,90 union all
select '04',3,90 union all

select '05',1,90
则查询结果为02呀,但是你们查出来的结果都是把03,04也查进来了呢?求解哟,本人sql太弱
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([s#] varchar(2),[c#] int,[grade] int)
insert [test]
select '01',1,80 union all
select '01',2,90 union all
select '02',1,80 union all
select '02',2,80 union all
select '03',1,79 union all
select '03',2,80 union all
select '03',3,90

with t
as(
select COUNT(1)over(partition by [s#]) px,*
from test
)
select s# from t a
where exists(
select 1 from t b where a.px=b.px and a.s#<>b.s#
)
and a.[c#] in(select [c#] from test where s#='02')
and a.s#<>'02'
孤独加百列 2012-05-30
  • 打赏
  • 举报
回复

IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')
BEGIN
DROP TABLE tba
END
GO
CREATE TABLE tba
(
s VARCHAR(10),
c INT,
grade INT
)

GO

INSERT INTO tba
SELECT '01', 1, 80 UNION
SELECT '01', 2, 90 UNION
SELECT '02', 1, 80 UNION
SELECT '02', 2, 80 UNION
SELECT '03', 1, 79 UNION
SELECT '03', 2, 80 UNION
SELECT '03', 3, 90

SELECT * FROM tba AS A
WHERE EXISTS (SELECT 1 FROM tba WHERE s = '02' AND c = A.c) AND S NOT IN(
SELECT s FROM tba AS A
WHERE NOT EXISTS (SELECT 1 FROM tba WHERE s = '02' AND c = A.c))

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧