查询选修了所有课程的学生1、首先查询出所有的课程select kch_id from kcb2、查询出课程的数目select count(kch_id) from kcb3、查询出课程在所有课程中的学生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)---------------------->学号,姓名
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)---------------------->学号,姓名
--方法1 根据数量来完成
select student.* from student where S# in
(select S# from sc group by S# having count(1) = (select count(1) from course))
--方法2 使用双重否定来完成
select t.* from student t where t.S# not in
(
select distinct m.S# from
(
select S# , C# from student , course
) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#)
)
--方法3 使用双重否定来完成
select t.* from student t where not exists(select 1 from
(
select distinct m.S# from
(
select S# , C# from student , course
) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#)
) k where k.S# = t.S#
)
--创建测试数据
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)
goselect 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 行)
*/
select kcb.kch_id from kcb
)
而且这句是多余的。
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#)) 这个有点迷,能解释一下么 谢谢
select 1 from course c where not exists(select 1 from sc where sc.c# = c.c# and sc.s# = s.s#))
我纠结啊,不明白
1. select 1 from sc where sc.c# = c.c# and sc.s# = s.s# 所有学了课程的
2.select 1 from course c where not exists(select 1 from sc where sc.c# = c.c# and sc.s# = s.s#) 一门课程都没学
3.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#))学了所有课程