项目中有这样一个需求,把页面分成一个一个的板块,然后统计各个板块在30天内的点击数,生成一个报表,如下图:
mysql获取数据的代码:
//结束时间 年月日
$endTime = date('Y-m-d');
//开始时间 年月日 前30天日期
$startTime = date('Y-m-d', (strtotime($endTime) - 30*24*60*60));
//循环的初始值 秒
$start = strtotime($startTime);
//循环的最大值 秒
$end = strtotime($endTime);
//步长 一天 循环的时候按一天的时间作为步长
$step = 24*60*60;
//sql语句 按点击代码 分组查询 按板块编号和点击的代码升序排序
$groupByHitCodeSql = "select type_id,hit_code,hit_name,";
for($i=$start; $i<$end; $i+=$step){
$time = date('Y-m-d', $i);
$groupByHitCodeSql .= " sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '{$time}' then `hit_num` else 0 end) as '$time',";
}
$groupByHitCodeSql = trim($groupByHitCodeSql, ',');
$groupByHitCodeSql .= " from tableName where `hit_date`>='{$startTime}' and `hit_date`<='{$endTime}' group by hit_code order by type_id asc,hit_code asc";
//运行程序生成代码
select type_id,hit_code,hit_name,
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-11' then `hit_num` else 0 end) as '2012-11-11',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-12' then `hit_num` else 0 end) as '2012-11-12',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-13' then `hit_num` else 0 end) as '2012-11-13',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-14' then `hit_num` else 0 end) as '2012-11-14',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-15' then `hit_num` else 0 end) as '2012-11-15',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-16' then `hit_num` else 0 end) as '2012-11-16',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-17' then `hit_num` else 0 end) as '2012-11-17',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-18' then `hit_num` else 0 end) as '2012-11-18',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-19' then `hit_num` else 0 end) as '2012-11-19',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-20' then `hit_num` else 0 end) as '2012-11-20',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-21' then `hit_num` else 0 end) as '2012-11-21',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-22' then `hit_num` else 0 end) as '2012-11-22',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-23' then `hit_num` else 0 end) as '2012-11-23',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-24' then `hit_num` else 0 end) as '2012-11-24',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-25' then `hit_num` else 0 end) as '2012-11-25',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-26' then `hit_num` else 0 end) as '2012-11-26',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-27' then `hit_num` else 0 end) as '2012-11-27',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-28' then `hit_num` else 0 end) as '2012-11-28',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-29' then `hit_num` else 0 end) as '2012-11-29',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-11-30' then `hit_num` else 0 end) as '2012-11-30',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-01' then `hit_num` else 0 end) as '2012-12-01',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-02' then `hit_num` else 0 end) as '2012-12-02',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-03' then `hit_num` else 0 end) as '2012-12-03',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-04' then `hit_num` else 0 end) as '2012-12-04',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-05' then `hit_num` else 0 end) as '2012-12-05',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-06' then `hit_num` else 0 end) as '2012-12-06',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-07' then `hit_num` else 0 end) as '2012-12-07',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-08' then `hit_num` else 0 end) as '2012-12-08',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-09' then `hit_num` else 0 end) as '2012-12-09',
sum(case when DATE_FORMAT(`hit_date`,'%Y-%m-%d') = '2012-12-10' then `hit_num` else 0 end) as '2012-12-10'
from juren_shouyes
where `hit_date`>='2012-11-11' and `hit_date`<='2012-12-11'
group by hit_code
order by type_id asc,hit_code asc
//数据库中运行 生成如下表格