非常感谢dawugui及各位朋友在上两贴的帮忙。
现在我这个查询涉及到两个表的关联问题,目前应该是没有关联上,如下,C表对查询产生的影响仅限于c.scoursename,所以查询出来的结果是各科中最大的,而不是每一科的分数。
代码:select b.name as 姓名,
max(case c.scoursename when '语文' then a.descore  end)  语文,
max(case c.scoursename when '数学' then a.descore  end)  数学,
max(case c.scoursename when '英语' then a.descore  end)  英语,
max(case c.scoursename when '政治' then a.descore  end)  政治,
max(case c.scoursename when '几何' then a.descore  end)  几何,
max(case c.scoursename when '物理' then a.descore  end)  物理,
max(case c.scoursename when '化学' then a.descore  end)  化学,
max(case c.scoursename when '生物' then a.descore  end)  生物,
max(case c.scoursename when '历史' then a.descore  end)  历史,
max(case c.scoursename when '地理' then a.descore  end)  地理
from t_card_score a,t_stu b,t_card_course c
where
a.nstuid=b.id and
a.ncouexamid in (select id from t_card_examcou where nclassid=19 and nexamid=7) and
a.nstuid in (select id from t_stu where nclassid=19)
group by b.name
order by b.name
查询结果图:结果是显示的成绩是每一个学生所有课程中分数最高的成绩。我不知道怎么关联C表中的字段到select中

解决方案 »

  1.   

    我仔细看了dawugui关于行列转换的代码,但是没能举一反三···
      

  2.   

    select b.name as 姓名,
    max(case c.scoursename when '语文' then a.descore  end)  语文,
    max(case c.scoursename when '数学' then a.descore  end)  数学,
    max(case c.scoursename when '英语' then a.descore  end)  英语,
    max(case c.scoursename when '政治' then a.descore  end)  政治,
    max(case c.scoursename when '几何' then a.descore  end)  几何,
    max(case c.scoursename when '物理' then a.descore  end)  物理,
    max(case c.scoursename when '化学' then a.descore  end)  化学,
    max(case c.scoursename when '生物' then a.descore  end)  生物,
    max(case c.scoursename when '历史' then a.descore  end)  历史,
    max(case c.scoursename when '地理' then a.descore  end)  地理
    from t_card_score a,t_stu b,t_card_course c,t_card_examcou d
    where a.nstuid=b.id and a.ncouexamid=d.ncouexamid and c.ncourseid=d.ncourseid
    and d.nclassid=19 and d.nexamid=7
    and b.nclassid=19
    group by b.name
    order by b.name
    试试最好搞点数据出来,这样比较容易解决
      

  3.   

    把數據貼出來
    方法:
    http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
      

  4.   

    需求:老师查看本班学生成绩及名次
    姓名  语文  数学 英语  名次
    ×××    ××  ××  ××    ××
    ×××    ××  ××  ××    ××有几个表:
    成绩表    t_card_score:ncouexamid(课程在考试中的ID),nstuid(学生ID),descore(分数)
    考试课程表 t_card_examcou:ncouexamid,nexamid,ncourseid(课程号),nclassid,课程表 t_card_course:ncourseid,name
    学生表 t_stu:nstuid,name,nclassid