我有两个桌子交易与费用
我试图做出明智的日期声明,在“交易”表中,我已经收到所有费用,在“费用”表中我已经拥有所有费用。
所以我尝试了如下查询
SELECT date(t1.created) as Receive_date, date(t2.created) as Expenses_date, t1.amount as ReceiveAmount, t2.amount as ExpensesAmount
FROM transactions as t1
LEFT JOIN expenses as t2 ON date(t1.created) = date(t2.created)
UNION
SELECT date(t1.created) as Receive_date, date(t2.created) as Expenses_date, t1.amount as ReceiveAmount, t2.amount as ExpensesAmount
FROM transactions as t1
RIGHT JOIN expenses as t2 ON date(t1.created) = date(t2.created)
这里是日期2019-11-12
,我有3笔收到的款项,但我有1笔开支。对于日期2019-11-12
中的费用,我得到的金额是相同金额的3倍。
我还需要将两个日期字段组合为一个日期字段。
经过下面的子查询后尝试
SELECT date(t.created),
t.amount,
ex.amount
FROM `transactions` as t
LEFT JOIN(
SELECT sum(e.amount) as amount, created
FROM expenses as e
) as ex
ON date(ex.created) = date(t.created)
UNION
SELECT date(ex2.created),
t.amount,
ex2.amount
FROM `transactions` as t
RIGHT JOIN(
SELECT sum(e.amount) as amount,created
FROM expenses as e
) as ex2
ON date(ex2.created) = date(t.created)
我在输出以下]
我的预期输出
date ReceiveAmount ExpensesAmount
2019-10-15 500 NULL
2019-11-06 500 NULL
2019-11-12 600 100
2019-12-11 50 NULL
2020-01-01 100 NULL
2019-11-01 NULL 500
我有两个表交易和费用我试图做一个日期明智的声明,在交易表中,我所有的收款额都在费用表中,我有所有的费用额。所以我有...
它应该起作用。