1. 程式人生 > >MYSQL之資料報表

MYSQL之資料報表

SELECT f.month AS month, f.amount AS amount
    FROM (
        SELECT *
        FROM (
            SELECT month(t_dissipate) AS months, SUM(t_amount) AS amount
            FROM discuz.transaction
            WHERE year(t_dissipate) = '2018'
            GROUP BY month(t_dissipate)
        ) t
            RIGHT JOIN (
                SELECT x.month AS month
                FROM discuz.months x
                UNION
                SELECT month(t_dissipate) AS months
                FROM discuz.transaction
                GROUP BY month(t_dissipate), t_amount
            ) d
            ON t.months = d.month
    ) f

months表: 月份 int型別 1-12

transaction:資料表   :新增時間  t_dissipate datetime 若干條

            :數量  amount  int  對應新增時間條數