我遇到了问题。经过几个小时的努力,我无法找到合适的解决方案。
首先我想做什么: 我有两个销售和购买表都有根据日期的数据。
sale1 | 25-07-14 | 2500
sale2 | 26-07-14 | 2200
sale3 | 27-07-14 | 0
sale4 | 27-07-14 | 1500
sale5 | 28-07-14 | 1000
采购:
purchase1 | 25-07-14 | 0
purchase2 | 26-07-14 | 1000
purchase3 | 27-07-14 | 2500
purchase4 | 28-07-14 | 0
purchase5 | 28-07-14 | 100
现在我想显示我的数据:
Date| sale(sum) | purchase(sum)
Date| sale(sum) | purchase(sum)
Date| sale(sum) | purchase(sum)
Date| sale(sum) | purchase(sum)
我想从两个表中获取有关日期的数据。
这就是我的尝试:
SELECT date, sum(purchase_remaining) as a FROM purchase group by date
UNION
SELECT sale_date, sum(sale_balance) as b FROM sale group by sale_date
此查询的结果:
date | purchase
date | purchase
date | purchase
date | sale
date | sale
date | sale
任何想法我怎样才能做到这一点。
答案 0 :(得分:3)
加入表格。
select date, sum(purchase_remaining) a
, sum(sale_balance) b
from purchase join sale on purchase.date = sale.date
group by date