请sql高手看一下这道题怎么做

2024-12-29 01:15:04
推荐回答(3个)
回答1:

LZ这么难的题目就放蛋帖,这个。。。。
--------------------------------------------------------------------------------------
declare @tb table(a varchar(2),s varchar(50))
insert into @tb select '01', '历史'
insert into @tb select '01', '历史'
insert into @tb select '01', '地震学'
insert into @tb select '01', '宏观经济'
insert into @tb select '02', '地震学'
insert into @tb select '02', '唐诗'
insert into @tb select '03', '历史'
insert into @tb select '03', '宏观经济'
insert into @tb select '03', '地震学'
insert into @tb select '03', '地震学'
insert into @tb select '04', '历史'
insert into @tb select '04', '宏观经济'
insert into @tb select '04', '地震学'
insert into @tb select '04', '电子信息'
insert into @tb select '06', '历史'
insert into @tb select '06', '宏观经济'

select A.a as 学号 from @tb A
left join
(select distinct * from @tb where a='01') B on(A.s=B.s)
where A.a<>'01'
group by A.a
having count( distinct A.s)=(select count(distinct K.S) from @tb K where K.a='01' )
-----------------
返回结果
03
-------------------------------------------------
应该使用内连接,左连接是选课数目相同,内联是完全相同。
select A.a as 学号 from @tb A
INNER join
(select distinct * from @tb where a='01') B on(A.s=B.s)
where A.a<>'01'
group by A.a
having count( distinct A.s)=(select count(distinct K.S) from @tb K where K.a='01' )

回答2:

select distinct 学号 from sc where 课程=(select 课程 from sc where 学号=01);

回答3:

select 学号 from sc where 课程 in (select 课程 from sc where 学号=01) group by 学号