原表:
id fenlei time
1 分类1 20130316
2 分类2 20130316
3 分类3 20130317
4 分类2 20130317
5 分类3 20130318
需要查上表,得到结果插入新表
新表结构:
id fenlei_1 fenlei_2 fenlei_3 date
1 1 1 0 20130316
2 0 1 1 20130317
3 0 0 1 20130317
即要得到每个分类在每天各自消息数
select time,sum(if(fenlei='分类1',1,0 )),
sum(if(fenlei='分类2',1,0 )),sum(if(fenlei='分类3',1,0 ))
from tt
group by time
2.案例2
select DATE_FORMAT(created_time,'%Y-%m-%d') as '日期',
sum(if(product='毛巾',price,0)) as '毛巾',
sum(if(product='枕头',price,0)) as '枕头'
from trade
GROUP BY DATE_FORMAT(created_time,'%Y-%m-%d');