各位 大侠hao菜鸟问一个问题表1如何通过查询得到如同表2的查询结果表1               
Year Month Rate
2001 1 0.1
2001 2 0.2
2001 3 0.3
2001 4 0.4
2002 1 0.11
2002 2 0.22
2002 3 0.33
2002 4 0.44 表2
Year M1 M2 M3 M4
2001 0.1 0.2 0.3 0.4
2002 0.11 0.22 0.33 0.44

解决方案 »

  1.   

    select year,
    sum(case when month=1 then rate end) as M1,
    sum(case when month=2 then rate end) as M2,
    sum(case when month=3 then rate end) as M3,
    sum(case when month=4 then rate end) as M4
    from tb1
    group by year
      

  2.   

    http://blog.csdn.net/acmain_chm/article/details/4283943
    MySQL交叉表
    在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx(  id int primary key,  c1 c...
      

  3.   

    select year,
     sum(if(month=1,rate,0)) as M1,
    sum(if(month=2,rate,0))as M2,
    sum(if(month=3,rate,0)) as M3,
    sum(if(month=4,rate,0)) as M4
     from tb1
     group by year