逐月累计订单数量

set @order_num = 0;

SELECT (@order_num := @order_num + t.totalOrder) AS tnum
FROM(
select IFNULL(tso.totalPay,0) AS totalOrder
        from month_view mv
        LEFT JOIN (
            SELECT
            DATE_FORMAT(tso.add_time,'%Y-%m') yearMonth,
            SUM(tso.num) totalPay
            FROM t_sale_order tso
            WHERE tso.delete_flag = 0
            AND DATE_FORMAT(tso.add_time,'%Y-%m') >   DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 12 MONTH),'%Y-%m')
            GROUP BY yearMonth
        ) tso ON mv.mon = tso.yearMonth
        GROUP BY mv.mon
) t

 

这是两条sql语句,在代码中,应该是两个xml语句

set 设置初始值为0

month_view 为查询近十二个月的视图,上篇博客中的内容

t_sale_order 为销售订单表

posted @ 2020-05-19 09:54  朝暮的小知识  阅读(305)  评论(0编辑  收藏  举报