两个不同的表日期列应该相同,金额列应该不同,如何计算每月的支出金额?

时间:2016-12-30 11:45:44

标签: mysql

SELECT month(date), SUM(amount) as amount
FROM expendituretotal
GROUP BY month(date);

我想要这样的输出:

month    amount1   amount2
---------------------------
June       2000      3000

1 个答案:

答案 0 :(得分:0)

正确答案。

SELECT e.month
    ,e.expp AS amount
    ,e1.depp AS amount
FROM (
     SELECT month(DATE) AS month
           ,SUM(amount) AS expp
     FROM expendituretotal
     GROUP BY month(DATE)
     ) e
    ,(
    SELECT month(DATE) AS month
          ,SUM(amount) AS depp
    FROM totalamount
    GROUP BY month(DATE)
    ) e1
WHERE e.month = e1.month