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

sql查询问题.
学生表:student(学号,姓名,课程编号)
课程表:course(课程编码,课程名)
求将选修了全部课程的学生查询出来.
麻烦大家帮我想想.  



------解决方案--------------------
create table student (id int,name varchar(8),courseid int)
create table course (courseid int,coursename varchar(8))

select id as '学号 ',name as '姓名 ' from student
group by id,name having count(1) = (select count(1) from course)
------解决方案--------------------
---方法1
Select Distinct 学号,姓名 From student As A Where Not Exists
(Select 1 From course As B Where Not Exists
(Select 1 From student Where 学号=A.学号 And 课程编号=B.课程编码))
---方法2
Select 学号,姓名
From student
Group By 学号,姓名 Having Count(1)=(Select Count(Distinct 课程编码) From course)
------解决方案--------------------
没有数据没有看懂你的意思
我做了个简单的例子 你看是不是这个意思
declare @t table(id int,name varchar(20),bh int)
insert into @t
select 1, 'zhangsan ',1
union all select 1, 'zhangsan ',2
union all select 1, 'zhangsan ',3
union all select 2, 'lisi ',1
union all select 2, 'lisi ',2
union all select 2, 'lisi ',3
union all select 2, 'lisi ',4
union all select 3, 'wangwu ',1
union all select 3, 'wangwu ',2
union all select 3, 'wangwu ',3
union all select 3, 'wangwu ',4

declare @b table(bh int,kc varchar(20))
insert into @b
select 1, 'shuxue '
union all select 2, 'yuwen '
union all select 3, 'yingyu '
union all select 4, 'huaxue '

select id,name from @t group by id,name having count(bh)=(select count(*)from @b)



------解决方案--------------------
学生表:student(学号,姓名,课程编号)
课程表:course(课程编码,课程名)
求将选修了全部课程的学生查询出来.
麻烦大家帮我想想.

select * from 学生表 where 学号 in
(select 学号 from 学生表 group by 学号 having count(*) = (select count(*) from 课程表))