按月从两个mysql表执行计算

时间:2015-09-03 06:06:54

标签: mysql constraints

我有两张桌子,一张收入,另一张用于支出

收入

id date                  amount person_id
1  2015-08-03 00:00:00   100    1
2  2015-09-03 13:32:10   200    2

费用

id date                  amount person_id
1  2015-09-03 13:32:43   12     1
2  2015-08-03 13:32:56   20     2
3  2015-08-03 13:35:55   17     2
4  2015-09-03 13:36:20   12     2

我想在mysql中做的是按月计算每个人的收入减去费用的余额。

MY SQL声明

SELECT
e.person_id,
Concat(MonthName(e.date), " ", Year(e.date)) AS Month,
SUM(e.amount) AS "Total Expenses",
SUM(i.amount) AS "Total Income",
SUM(i.amount)- SUM(e.amount) AS "Balance"
FROM
a_expenses e
LEFT OUTER JOIN 
a_income i ON e.person_id=i.person_id
 GROUP BY e.amount, e.person_id, Month(e.date)

RESULT

person_id Month           Total Expenses Total Income Balance
1         September 2015  12             100          88
2         September 2015  12             200          188
2         August 2015     17             200          183
2         August 2015     20             200          180

如何设置基于每个人月份总和计算收入的约束,结果仅显示该人每月一次。

2 个答案:

答案 0 :(得分:0)

您假设费用表具有每月的值或收入表中没有月份,而且费用表中也没有。您无法保证这些条件始终存在(恕我直言)

要避免这种情况,您需要使用一个列出人员的表格,如下所示:

SELECT
      p.id
    , p.name
    , Concat(MonthName(COALESCE(e.mnth, i.mnth)), " ", YEAR(COALESCE(e.yr, i.yr))) AS Month
    , SUM(COALESCE(e.expense_amount,0)) AS "Total Expenses"
    , SUM(COALESCE(i.income_amount,0)) AS "Total Income"
    , SUM(COALESCE(i.income_amount,0) - COALESCE(e.expense_amount,0)) AS "Balance"
FROM persons p
      LEFT OUTER JOIN (
            SELECT
                  person_id
                , MONTH(date) AS mnth
                , YEAR(date) AS yr
                , SUM(amount) AS expense_amount
            FROM expenses
            GROUP BY person_id
                   , MONTH(date)
                   , YEAR(date)
      ) e ON p.id = e.person_id
      LEFT OUTER JOIN (
            SELECT
                  person_id
                , MONTH(date) AS mnth
                , YEAR(date) AS yr
                , SUM(amount) AS income_amount
            FROM income 
            GROUP BY person_id
                   , MONTH(date)
                   , YEAR(date)
      ) i ON p.id = i.person_id and e.mnth = i.mnth and e.yr = i.yr

答案 1 :(得分:0)

试试这个

SELECT
e.person_id,
e.Month,
SUM(e.amount) AS "Total Expenses",
SUM(i.amount) AS "Total Income",
SUM(i.amount)- SUM(e.amount) AS "Balance"
FROM
a_income i
LEFT OUTER JOIN 
(select sum(amount) as amount,person_id,Concat(MonthName(date), " ",
Year(i.date)) as Month from a_expenses group by person_id, Month(date)) 
 as  e ON e.person_id=i.person_id
 GROUP BY e.person_id, e.Month

使用示例SqlFiddle http://sqlfiddle.com/#!9/b3d4e/18

进行检查