MySQL写近一年每个月的统计
案例
SQL:
-- 外层SQL求同月不同情况的总和
SELECT sum(one + two+three+four+ five+six+seven+eight) as total ,createDate as month FROM (
-- 按月份获取每月不同情况的数据
SELECT sum(gift = 0.38) one ,sum(gift = 0.58) two,sum( gift = 0.68 ) three ,sum(gift =0.88) four, sum(gift = 38) five,sum(gift = 5) six,sum(gift = 10) seven ,sum(gift = 0) eight,DATE_FORMAT(create_time, '%Y-%m' ) as createDate
FROM game
-- 按时间月份分组
group by createDate
-- 按月份倒序取前12条,实现取近12个月的数据
order by createDate desc limit 12
) as A group by createDate
效果:


该SQL示例展示了如何在MySQL中统计近一年每个月的不同情况总和,通过对game表中的数据进行分组,按月份计算各类型gift的频数,然后对每个月的结果进行求和。使用DATE_FORMAT处理日期并按降序排列,最后限制结果为最近12个月的数据。
921

被折叠的 条评论
为什么被折叠?



