首页 > 代码库 > 根据条件合并结果集

根据条件合并结果集

原文 根据条件合并结果集

第一个查询是将三个表中的列通过键连接起来,并将查询结果重命名为别名aaa;

最后将两个查询的结果集aaa,bbb,通过 aaa.student_id = bbb.studentid连接起来。

 

select aaa.*,bbb.required,bbb.elective from(select c.student_id,c.chinese_name,c.user_class, sum(decode(b.coursetype,1,a.credit,0)) get_required_total,sum(decode(b.coursetype,0,a.credit,0)) get_elective_total, sum(decode(b.coursetype,1,a.coursecredit,0)) reg_required_total,sum(decode(b.coursetype,0,a.coursecredit,0)) reg_elective_totalfrom sm_course_student a,sm_courses b,sm_students cwhere a.course_id=b.courseid and a.student_id=c.student_id  and c.user_class like 10SH% group by c.student_id,c.chinese_name,c.user_class)aaa,(select  studentid,required,elective from sm_school_roll where classname like 10SH%)bbbwhere aaa.student_id = bbb.studentid

 

根据条件合并结果集