各位高手,我建了一个数据库,其中有7张表,其中有一种表记录成绩,为score表,表中有5列,第一列自动排序score_id,第二列为学生学号student_no,第三列为课程号course_no,第四列为具体的分数score,第五列为是否处于可用状态active_status,
分数表,score
score_id   student_no course_no   score   active_status            
1    2406070425    002       95 Y
2    2406070425    004       88 Y
3    2406070401    004       78 Y
4    2406070405    004       85 Y
5    2406070424    004       75 N
6    2406070414    005       70 Y
7    2406070424    004       78 N
8    2406070425    006       71 Y
9    2406070425    006       88 Y
10    2406070425    004       65 N
示例数据,我想为各位高手,怎么从这张表中选出一个人,也就是同一个学号的人的所有课程的成绩,准备在ListControl中输出,
课程表,course
course_id  course_no   course_name  course_hour  course_credit  active_status
1      001   C++         48        5      Y
2      002 大学英语 54        5 Y
3      003 大学物理 48        4 Y
4      004 Java         46        4 Y
5      005 工程经济 36        3 Y
6      006 信息检索 38        3 Y
格式为:         学号    姓名   课程1   课程2   课程3 ;;;;最好还能求出每个人的总分和平均分,可以吗?
急用啊,我在这里先谢谢大家了!

解决方案 »

  1.   

    求出每个人的总分和平均分
    select student_no,sum(score) as 总分,avg(score) as 平均分
    from score
    group by student_no
      

  2.   

    http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html?69321
      

  3.   

    呵呵,谢谢大家啊,不过我是在MFC程序中ListControl控件中显示出来,我很菜的,希望大家详细说明一下,另外能不能直接给出语句呢,呵呵
           m_list.DeleteAllItems();
    CString strSQL;

           strSQL.Format("  ");   这里SQL语句该怎么写呢?
            
            m_recordSet.Open(CRecordset::forwardOnly,strSQL);
    for(int i=0;i<m_recordSet.GetRecordCount();i++){
    CString temp;
    m_recordSet.GetFieldValue("score_id",temp);
    m_list.InsertItem(i,temp);
    m_recordSet.GetFieldValue("student_no",temp);
    m_list.SetItemText(i,1,temp);
                    m_recordSet.GetFieldValue("score",temp);
    m_list.SetItemText(i,2,temp);
    m_recordSet.GetFieldValue("active_status",temp);
    m_list.SetItemText(i,3,temp);
                    m_recordSet.MoveNext();
    }
    m_recordSet.Close();或者直接点,呵呵,用ListControl控件怎么显示我要求的结果呢,就是在列表中显示学号,姓名,各科分数,总分,平均分 , 代码怎么写呢,麻烦了呵呵,要求有点小多啊,不过赶着交呢,没办法了,大家帮帮忙吧!
      

  4.   


    declare @sql varchar(max)
    set @sql='select a.student_no,a.studentname'
    select @sql=@sql+',['+course_name+']=max(case course_name when '''+course_name+''' then score else 0 end)'
    from course
    set @sql=@sql+',总分=sum(score),平均分=avg(score) from score a,student b where a.student_no=b.student_no group by a.student_no,a.studentname'
    exec(@sql)
      

  5.   

    --假设学生信息表为student表,studentname为学生姓名--> 测试数据: [student]
    if object_id('[student]') is not null drop table [student]
    create table [student] (student_no bigint,studentname varchar(6))
    insert into [student]
    select 2406070425,'刘德华' union all
    select 2406070401,'张学友' union all
    select 2406070405,'郭富城' union all
    select 2406070424,'黎明'--> 测试数据: [score]
    if object_id('[score]') is not null drop table [score]
    create table [score] (score_id int,student_no bigint,course_no varchar(3),score int,active_status varchar(1))
    insert into [score]
    select 1,2406070425,'002',95,'Y' union all
    select 2,2406070425,'004',88,'Y' union all
    select 3,2406070401,'004',78,'Y' union all
    select 4,2406070405,'004',85,'Y' union all
    select 5,2406070424,'004',75,'N' union all
    select 6,2406070414,'005',70,'Y' union all
    select 7,2406070424,'004',78,'N' union all
    select 8,2406070425,'006',71,'Y' union all
    select 9,2406070425,'006',88,'Y' union all
    select 10,2406070425,'004',65,'N'
    --> 测试数据: [course]
    if object_id('[course]') is not null drop table [course]
    create table [course] (course_id int,course_no varchar(3),course_name varchar(8),course_hour int,course_credit int,active_status varchar(1))
    insert into [course]
    select 1,'001','C++',48,5,'Y' union all
    select 2,'002','大学英语',54,5,'Y' union all
    select 3,'003','大学物理',48,4,'Y' union all
    select 4,'004','Java',46,4,'Y' union all
    select 5,'005','工程经济',36,3,'Y' union all
    select 6,'006','信息检索',38,3,'Y'
    declare @sql varchar(max)
    set @sql='select a.student_no,a.studentname'
    select @sql=@sql+',['+course_name+']=max(case course_no when '''+course_no+''' then score else 0 end)'
    from course
    set @sql=@sql+',总分=sum(score),平均分=avg(score) from student a,score b where a.student_no=b.student_no group by a.student_no,a.studentname'
    exec(@sql)
    --结果:
    student_no           studentname C++         大学英语        大学物理        Java        工程经济        信息检索        总分          平均分
    -------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    2406070401           张学友         0           0           0           78          0           0           78          78
    2406070405           郭富城         0           0           0           85          0           0           85          85
    2406070424           黎明          0           0           0           78          0           0           153         76
    2406070425           刘德华         0           95          0           88          0           88          407         81
      

  6.   

    真的很谢谢你啊,在SQL中查询中确实可以实现想要的功能,可我得在MFC中的ListControl中把表给显示出来,它能直接显示出SQL查出的表吗?
    我用ODBC连接的数据库,这该怎么显示呢?
    BOOL CSummaryDlg::OnInitDialog(void)   //ListControl的初始化函数
    {
        
        CDialog::OnInitDialog();
    //如果没有打开数据库,则打开数据库
    if(!m_database.IsOpen())
    {
    m_database.Open(_T("studentscore"));
        m_recordSet.m_pDatabase=&m_database;
    }
    //初始化里CList 控件的和header
    m_list.InsertColumn(0,"学号");
            m_list.InsertColumn(1,"学生名");
            m_list.InsertColumn(2,"C++");
    m_list.InsertColumn(3,"大学英语");
    m_list.InsertColumn(4,"大学物理");
    m_list.InsertColumn(5,"Java");
    m_list.InsertColumn(6,"工程经济");
    m_list.InsertColumn(7,"信息检索");
    //设置header的宽度
            RECT rectList;
    m_list.GetWindowRect(&rectList);
    int wid=rectList.right-rectList.left-8;
    for(int i=0;i<8;i++)
    m_list.SetColumnWidth(i,wid/8);
            m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
    //调用refreshList()初始化CList中的数据
    RefreshList();
    return true;
    } void CSummaryDlg::RefreshList(void)
    {
       //删除CList的所有内容
    m_list.DeleteAllItems();
    //打开记录集
    CString strSQL;
    !!!!!!!!!!!!!strSQL.Format("");   就这个format后面的字符串该写什么SQL语句呢,
            m_recordSet.Open(CRecordset::forwardOnly,strSQL);
    //将记录集显示到 CList中
    for(int i=0;i<m_recordSet.GetRecordCount();i++){
    CString temp;
    m_recordSet.GetFieldValue("student_no",temp);
    m_list.InsertItem(i,temp);
    m_recordSet.GetFieldValue("student_name",temp);
    m_list.SetItemText(i,1,temp);
                    m_recordSet.GetFieldValue("score",temp);
    m_list.SetItemText(i,2,temp);
    //m_recordSet.GetFieldValue("active_status",temp);
    //m_list.SetItemText(i,3,temp);
                    m_recordSet.MoveNext();
    }
    //关闭数据库
    m_recordSet.Close();
    }被我这样的新手问很烦吧,呵呵,不过还是希望你们可以帮我解决啊!