W雨文

学习java

导航

MySQL数据组装,根据前端传入当前年月往前推12月,当月有数据带着数据,若当月无数据,赋值0

SELECT
a.*
FROM
(
SELECT
date_format( created_time, '%Y-%m' ) AS cmonth,
count( `created_time` ) AS totalQuantity
FROM
`tb_ter_terminal`
GROUP BY
cmonth
HAVING
cmonth BETWEEN DATE_SUB(#{vo.yearMonth}, interval 12 MONTH) and #{vo.yearMonth}
UNION ALL
SELECT
date_format( ( @date := DATE_ADD( @date, INTERVAL 1 MONTH )), '%Y-%m' ) AS cmonth,
0 AS totalQuantity
FROM
`tb_ter_terminal`,(
SELECT
@date := DATE_SUB(#{vo.yearMonth}, interval 12 MONTH) ) temp
WHERE
@date <![CDATA[<]]> #{vo.yearMonth} ) a GROUP BY a.cmonth

posted on 2023-02-24 10:28  W雨文  阅读(26)  评论(0编辑  收藏  举报