选择所有课程的学生名称
三张表,要查询出选择了所有课程的学生的成绩
Example:
std表
std_id std_name
0001 alex
0002 bill
0003 cliton
0004 duke
course表
course_id course_name
2001 english
2002 math
2003 physics
score表
std_id course_id score
0001 2001 81
0002 2001 91
0001 2003 24
0003 2002 62
0001 2002 54
输出:
alex english 81
alex math 54
alex physics 91
问题点数:20、回复次数:11
Top
1 楼peng1014()回复于 2005-01-11 17:34:18 得分 0 select score.std_name,course.course_name,score from std,course,score
where std.std_id=score.std_id and course.course_id=score.course_id
Top
2 楼libin_ftsafe(子陌红尘:当libin告别ftsafe)回复于 2005-01-11 17:45:00 得分 0 select
a.std_id ,
a.std_name ,
b.course_id ,
c.course_name,
b.score
from
std a,
score b,
course c,
(select std_id,count(*) cnt from score) d,
(select count(course_id) cnt from course) e
where
a.std_id = b.std_id and a.std_id = d.std_id and d.cnt = e.cnt and c.course_id = b.course_id
Top
3 楼didoleo(冷月无声)回复于 2005-01-11 18:03:23 得分 10create table std
(std_id varchar(4), std_name varchar(100))
create table course
(course_id varchar(4), course_name varchar(100))
create table score
(std_id varchar(4), course_id varchar(4) , chengji int)
insert into std
select '0001' , 'alex' union all
select '0002' , 'bill' union all
select '0003' , 'cliton' union all
select '0004' , 'duke'
insert into course
select '2001' , 'english' union all
select '2002' , 'math' union all
select '2003' , 'physics'
insert into score
select '0001' , '2001' , 81 union all
select '0002' , '2001' , 91 union all
select '0001' , '2003' , 24 union all
select '0003' , '2002' , 62 union all
select '0001' , '2002' , 54
select distinct std.std_name,course.course_name,score.chengji
from std right join score on std.std_id=score.std_id left join course on score.course_id=course.course_id
where not exists
(select 1 from course where
not exists (select 1 from score where std_id=std.std_id and course_id=course.course_id))
-----------------------
alex english 81
alex math 54
alex physics 24
(所影响的行数为 3 行)
Top
4 楼playyuer(退休干部 卧鼠藏虫)回复于 2005-01-11 18:18:41 得分 0 双 not exists
Top
5 楼maxiaohui1212()回复于 2005-01-12 08:20:01 得分 0 老兄:
建个视图!你认为呢?
Top
6 楼lxysjl(流星雨)回复于 2005-01-12 09:13:07 得分 0 我也这么认为
Top
7 楼floatyzq(float)回复于 2005-01-12 09:35:42 得分 0 didoleo(冷月无声) 的完全正确
Top
8 楼maxiaohui1212()回复于 2005-01-12 09:36:27 得分 0 SELECT TOP 100 PERCENT dbo.std.std_name, dbo.course.course_name,
dbo.score.score, dbo.std.std_id
FROM dbo.std RIGHT OUTER JOIN
dbo.score ON dbo.std.std_id = dbo.score.std_id LEFT OUTER JOIN
dbo.course ON dbo.score.course_id = dbo.course.course_id
ORDER BY dbo.std.std_id
Top
9 楼seayar(习习)回复于 2005-01-12 09:51:17 得分 0 up
Top
10 楼SickNirvana(SickNirvana)回复于 2005-01-12 09:53:56 得分 10呵呵,这个问题跟我之前遇到的一模一样
前面写得太复杂了
最经典的做法还是双not exists
具体看这里:
http://community.csdn.net/Expert/topic/3696/3696046.xml?temp=.1676905
Top
11 楼winternet(冬天)回复于 2005-01-12 10:48:48 得分 0 select std_name,course_name,score from std as a inner join score as b on a.std_id=b.std_id
inner join course as c on b.course_id=c.course_id
where a.std_id in
(select std_id from score group by Std_ID having count(distinct course_id)=(select count(*) from course))
测试结果:
std_name course_name score
---------- ---------- ----------
alex english 81
alex math 54
alex physics 24
(3 row(s) affected)