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

求提取不合格成绩的SQL
已知:
表1:score
Stu_id   Lession_id   score
a             L1                     80
a             L2                     80
b             L1                     20
b             L2                     80
c             L2                     30
c             L2                     80
.             .                         .
.             .                         .
.             .                         .
表二:lession
Lession_id   Lession_des
L1                     1
L2                     2
L3                     3
.                       .
.                       .
.                       .
如何输出没有参加考试的学生的学号?考试科目以表二为准。格式如下:
Stu_id       Lession_des
a                     3
b                     3
.                     .
.                     .
.                     .
又如何输出课程的前三名的学号?,格式如下:
Lession_dex   第一名   第二名   第三名
1
2
3
.
.
.
新手,分不多,一个10分,请多多包涵!

------解决方案--------------------
--1
create table score(Stu_id varchar(10), Lession_id varchar(10), score int)
insert score select 'a ', 'L1 ', 80
union all select 'a ', 'L2 ', 80
union all select 'b ', 'L1 ', 20
union all select 'b ', 'L2 ', 80
union all select 'c ', 'L2 ', 30
union all select 'c ', 'L2 ', 80

create table lession(Lession_id varchar(10), Lession_des int)
insert lession select 'L1 ', 1
union all select 'L2 ', 2
union all select 'L3 ', 3

select tmp.Stu_id, tmp.Lession_des from
(
select * from lession as A
cross join (select distinct Stu_id from score) B
)tmp
left join score as B on tmp.Lession_id=B.Lession_id and tmp.Stu_id=B.Stu_id
where B.score is null

--result
Stu_id Lession_des
---------- -----------
a 3
b 3
c 1
c 3