我现在有如下表:   
  班级 语文  数学     英语     
  1班 80.6 86.0 82.5   
  2班 80.3 83.5 79.0   
  3班 84.5 91.0 87.5   
    
  行列转换成:   
    科目                1班             2班             3班         
  语文           80.6           80.3           84.5   
  数学           86.0           83.5           91.0   
  英语           82.5           79.0           87.5   
尽可能使用两个动态sql实现,尽可能使方法通用些,尽可能使代码效率高些,尽可能方法多些

解决方案 »

  1.   

    select 科目,
    sum(if(班级='1班',Score,0)) as `1班`,
    sum(if(班级='2班',Score,0)) as `2班`,
    sum(if(班级='3班',Score,0)) as `3班`
    from (
    select 班级,'语文' as 科目,语文 as Score from table1 
    union all
    select 班级,'数学' as 科目,数学 from table1 
    union all
    select 班级,'英语' as 科目,英语 from table1 
    ) t 
    group by 科目
      

  2.   

    我主要是想学一下动态sql,mysql的资料较少。。
      

  3.   

    动态的上次小梁好象给过一个例子了。MySQL的资料看官方文档足已。
    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html