这样的sql语句如何写?(100分)

Y

ylbee

Unregistered / Unconfirmed
GUEST, unregistred user!
学生表的结构
stu_no stu_name Grade Term
20020301 幸福 1 2

课程表的结构
course_no Course_name Course_credit
001 数学 3


成绩表的结构
stu_no Term Grade Course_no Number
20020301 1 1 001 65
20020301 1 1 002 70
20020304 2 1 007 50

补考成绩表的结构
Stu_no Course_no Term Grade BKcj1 bkcj2 bkcj3 memo
20020304 007 1 2 55 56 70 补考了3次
(在这里记录的学期、年级是最后一次补考的时间)

现在我想实现:
stu_no Stu_name Course_no Course_name Number bkcj1 bkcj2 bkcj3 memo grade term
20020304 碑石 007 历史 50 55 56 70 2 1
20020401 立嗣 008 物理 58

在这个视图中要求看到的字段就是上面那些。如果有补考记录的同学,都要列出来;同时有不及格课程,但还没有补考记录的同学,一样要列出来

望高手执教,谢谢!!
 
高手们多少给一点建议了
 
二次查询啊。
select distinct ……
from ……
where stu_no in
(select stu_no from 成绩表 where number<60 )
 
看这个符合你的要求不?可能条件不是太正确,自己再改吧!
------------------------------------
CREATE VIEW BuKaoTbl
AS
SELECT A.stu_no A.Stu_name B.Course_no B.Course_name C.Number D.bkcj1 D.bkcj2 D.bkcj3 D.memo C.grade C.term
FROM 学生表 A,课程表 B,成绩表 C,补考成绩表 D
WHERE (C.Number<60 AND C.stu_no=A.stu_no) AND (C.Course_no=B.Course_no)
OR (D.stu_no=A.stu_no AND D.Course_no=C.Course_no)
 
有一种右连接表的方法,不知道能不能实现
关键是:有一门不及格,但他还没有补考纪录,这里的处理,应该将他列出来,同时修改他补考
纪录后,应作为新纪录插入到补考数据表中
 
use union

select student_name
, course_name
, decode (sum(original_score), 0, '', to_char(sum(original_score))) as original_score
, decode (sum(first_makeup_score), 0, '', to_char(sum(first_makeup_score))) as first_makeup_score
, decode (sum(second_makeup_score), 0, '', to_char(sum(second_makeup_score))) as second_makeup_score
from
(
select st.name as student_name
, cs.name as course_name
, ts.score as original_score
, 0 as first_makeup_score
, 0 as seond_makeup_score
from
student st, course cs, transcript ts
where st.student_id = ts.student_id
and cs.course_id = ts.course_id
union
select st.name as student_name
, cs.name as course_nme
, 0 as original_score
, mts.first_makeup_score as first_makeup_score
, mts.seond_makeup_score as seond_makeup_score
from
student st, course cs, makeup_transcript mts
where st.student_id = mts.student_id
and cs.course_id = mts.course_id
)
group by student_name
, course_name
 
select 学生表,成绩表,补考成绩表
from a left join b on a.stu_no=b.stu_no
left join c on a.stu_no=c.stu_no
不过没有课程名,可用上面的结果再对应一次课程的
视图的修改比较麻烦,需要只有一个表的基础上才允许修改
 
顶部