SQL2005 在三个基本表个查询任意想要的数据

来源:互联网 发布:linux显示环境变量 编辑:程序博客网 时间:2024/05/23 07:22

 

昨天做了题:

    有三个表:学生表,课程表,成绩表  然后要查询每个学生的名字,数学,语文,历史等各科的成绩

      刚开始一看时觉得很简单,谁知道仔细一想,不是这边不对,就是那边不对

        真是晕了头了,真想放弃,可一想应该不难的题就想放弃,那以后怎做程序员啊

后来通过我不断的努力,不断的练,终于让我做出来了 如:

 

   首先把所有关联的数据放在一个临时表

 

            select stu.id,stu_name,course_name,score_score

               into temp

               from stu,course,score

               where  stu.id=score.sid and score.cid=course.id

        go

           select * from temp

       go

       再从这个临时表中查询你想要的数据

            select stu_name,数学=sum(case course_name when '数学' then score_score else 0 end),

                                      语文=sum(case course_name when '语文'then score_score else 0 end),

                                      '英语'=sum(case course_name when '英语' then score_score else 0 end)

                    --如果还有其它科,以此类推

            from temp

            group by stu_name

 

你不要用DATAREPORT,因为功能太少,你现在去下载ACTIVEREPORT2.0它支持报表的分栏打印

                                     

           

原创粉丝点击