我有两张名为费用和捐款的表格
表1
费
s.no expensedate expense
1 13/01/16 30
2 15/01/16 60
表2
s.no donationdate donation
15 14/01/16 30
18 19/01/16 10
Out Put required
Month Expense Donation
Jan 90 40
我可以像
一样单独获取输出Month expense
jan 90
或
Month donation
jan 40
但不能将它们连接在一起,这样可以提供所需的输出。
THX
答案 0 :(得分:1)
我会使用union all
和group by
:
select date_format(dte, '%Y-%m') as yyyymm,
coalesce(sum(expense), 0) as expenses,
coalesce(sum(donation), 0) as donations
from ((select expensedate as dte, expense, null as donation
from expenses
) union all
(select donationdate as dte, null, donation
from donations
)
) ed
group by date_format(dte, '%Y-%m')
order by yyyymm;
答案 1 :(得分:0)
我应该在第一时间显示代码抱歉, 表格1 选择 过期日期, 金额AS费用, ExpID 从费用 输出: 2016-05-07 400 1 2016-05-08 6779 2
表2 选择 DonationID, 捐款, DonationDate 来自捐赠 输出: 1 1 2016-04-08 2 200 2016-04-08 3 1000 2016-05-07 4 1200 2016-04-08 5 1 2016-03-08 6 500 2016-04-08 7 1000 2016-05-08
我不介意改变我的表格来创建它们之间的关系,这样可以很容易地获得所需的输出。
必需输出:
月捐款费用 3月1日 1901年4月 2000年5月7179
您的查询可以解决此问题,但我无法在查询中嵌入我的方案。 THX