解决方案 »

  1.   

    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...
      

  2.   

    多谢版主帮忙,看了这篇博文,可以解决我的问题的70%,但是现在还有如下问题需请教:
    SELECT classify as '-',
    #请注意?号
    IF((FROM_UNIXTIME(startDate,'%m')='01' || FROM_UNIXTIME(startDate,'%m')='12') && startDate >= UNIX_TIMESTAMP(CONCAT(2013,'-12-15')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-01-15')),?,0 ) AS '1月使用次数',
    sum(IF((FROM_UNIXTIME(startDate,'%m')='01' || FROM_UNIXTIME(startDate,'%m')='12') && startDate >= UNIX_TIMESTAMP(CONCAT(2013,'-12-15')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-01-15')),duration,0)) AS '1月合计',IF((FROM_UNIXTIME(startDate,'%m')='01' || FROM_UNIXTIME(startDate,'%m')='02') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-01-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-02-15')),?,0) AS '2月使用次数',
    sum(IF((FROM_UNIXTIME(startDate,'%m')='01' || FROM_UNIXTIME(startDate,'%m')='02') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-01-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-02-15')),duration,0)) AS '2月合计',IF((FROM_UNIXTIME(startDate,'%m')='02' || FROM_UNIXTIME(startDate,'%m')='03') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-02-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-03-15')),?,0) AS '3月使用次数',
    sum(IF((FROM_UNIXTIME(startDate,'%m')='02' || FROM_UNIXTIME(startDate,'%m')='03') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-02-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-03-15')),duration,0)) AS '3月合计',IF((FROM_UNIXTIME(startDate,'%m')='03' || FROM_UNIXTIME(startDate,'%m')='04') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-03-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-04-15')),?,0) AS '4月使用次数',
    sum(IF((FROM_UNIXTIME(startDate,'%m')='03' || FROM_UNIXTIME(startDate,'%m')='04') && startDate >= UNIX_TIMESTAMP(CONCAT(2014,'-03-16')) && startDate <= UNIX_TIMESTAMP(CONCAT(2014,'-04-15')),duration,0)) AS '4月合计',
    ...
    sum(duration) as '合计'
    from t GROUP BY classify
    我想得出这种结果//使用次数:其实就是添加一条记录就等于使用一次,?号处该怎么写?
    1月使用次数,1月合计,2月使用次数,2月合计,3月使用次数,3月合计...
              1  5              0    0           2    10