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

老师说:查询选修了所有课程的学生是个经典问题。我想到了更好的解决办法。请大家看看是否可以。
查询选修了所有课程的学生

1、首先查询出所有的课程

select kch_id from kcb

2、查询出课程的数目

select count(kch_id) from kcb

3、查询出课程在所有课程中的学生

select xh_id from cjb 

    where kch_id in (

 

    select kch_id from kcb

    )

4、对查询出的课程号进行分组,分组条件为countdistinct kch_id) = (

    select count(kch_id) from kcb

)

查询的出的结果即为选修了所有课程的学生

 

综上所述。即为:

select cjb.xh_id from cjb 
where cjb.kch_id in(
 select kcb.kch_id from kcb
)
GROUP BY cjb.xh_id
HAVING count(DISTINCT cjb.kch_id) = (
 SELECT count(kch_id) from kcb
)

 



各表分别为:

成绩表cjb(xh_id,kch_id) ---------------->学号,课程号

课程表kcb(kch_id,name)---------------->课程号,课程名

学生表xsb(id,name)---------------------->学号,姓名


------解决方案--------------------
--双重否定的另外一种方法,较我上面给出的好.
SQL code
--创建测试数据
create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))
insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')
insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')
insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')
insert into Student values('04' , N'李云' , '1990-08-06' , N'男')
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')
insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))
insert into Course values('01' , N'语文' , '02')
insert into Course values('02' , N'数学' , '01')
insert into Course values('03' , N'英语' , '03')
create table Teacher(T# varchar(10),Tname nvarchar(10))
insert into Teacher values('01' , N'张三')
insert into Teacher values('02' , N'李四')
insert into Teacher values('03' , N'王五')
create table SC(S# varchar(10),C# varchar(10),score decimal(18,1))
insert into SC values('01' , '01' , 80)
insert into SC values('01' , '02' , 90)
insert into SC values('01' , '03' , 99)
insert into SC values('02' , '01' , 70)
insert into SC values('02' , '02' , 60)
insert into SC values('02' , '03' , 80)
insert into SC values('03' , '01' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '03' , 80)
insert into SC values('04' , '01' , 50)
insert into SC values('04' , '02' , 30)
insert into SC values('04' , '03' , 20)
insert into SC values('05' , '01' , 76)
insert into SC values('05' , '02' , 87)
insert into SC values('06' , '01' , 31)
insert into SC values('06' , '03' , 34)
insert into SC values('07' , '02' , 89)
insert into SC values('07' , '03' , 98)
go

select s.* from student s where not exists(
select 1 from course c where not exists(select 1 from sc where sc.c# = c.c# and sc.s# = s.s#))

drop table  Student,Course,Teacher,SC

/*
S#         Sname      Sage                                                   Ssex       
---------- ---------- ------------------------------------------------------ ---------- 
01         赵雷         1990-01-01 00:00:00.000                                男
02         钱电         1990-12-21 00:00:00.000                                男
03         孙风         1990-05-20 00:00:00.000                                男
04         李云         1990-08-06 00:00:00.000                                男

(所影响的行数为 4 行)
*/