统计数量
SELECT YEAR(FROM_UNIXTIME(regdate)) 年,MONTH(FROM_UNIXTIME(regdate)) 月,COUNT(*) FROM `model_customer` WHERE 1=1 GROUP BY YEAR(FROM_UNIXTIME(regdate)),MONTH(FROM_UNIXTIME(adddate));
统计金额
SELECT YEAR(FROM_UNIXTIME(adddate)) 年,MONTH(FROM_UNIXTIME(regdate)) 月,SUM(total_amount) FROM `model_order` WHERE `pay_status`=1 GROUP BY YEAR(FROM_UNIXTIME(adddate)),MONTH(FROM_UNIXTIME(adddate));
按表头统计
select IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '1' THEN total_amount ELSE 0 END), 0) AS 一月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '2' THEN total_amount ELSE 0 END), 0) AS 二月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '3' THEN total_amount ELSE 0 END), 0) AS 三月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '4' THEN total_amount ELSE 0 END), 0) AS 四月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '5' THEN total_amount ELSE 0 END), 0) AS 五月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '6' THEN total_amount ELSE 0 END), 0) AS 六月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '7' THEN total_amount ELSE 0 END), 0) AS 七月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '8' THEN total_amount ELSE 0 END), 0) AS 八月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '9' THEN total_amount ELSE 0 END), 0) AS 九月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '10' THEN total_amount ELSE 0 END), 0) AS 十月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '11' THEN total_amount ELSE 0 END), 0) AS 十一月份, IFNULL(SUM(CASE MONTH(FROM_UNIXTIME(adddate)) WHEN '12' THEN total_amount ELSE 0 END), 0) AS 十二月份 FROM `model_order` WHERE YEAR(FROM_UNIXTIME(adddate))=2021;