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 createDateFROM game-- 按时间月份分组group by createDate -- 按月份倒序取前12条,实现取近12个月的数据order by createDate desc limit 12 ) as A group by createDate
效果:
来源地址:https://blog.csdn.net/Do_LaLi/article/details/129497371