我有两张销售和收据表
表结构和结果表结构如下。
销售
date | total
------------+-----------
2014-08-09 | 500
2014-07-08 | 50
2014-07-08 | 100
收据
date | net_amount
----------- +-----------------
2014-08-08 | 50
2014-08-08 | 50
预期结果
month total net_amount
-------+----------+---------------
07 | 150 |
08 | 500 | 100
QUERY
select MONTH(date),total,net_amount from (
select MONTH(date) as month,
sum(total) as total 2 as sort_col from
sale union all select MONTH(date) as month,
sum(net_amount) as net_amount,
1 as sort_col from receipt)
as a order by MONTH(date) desc, sort_col desc
答案 0 :(得分:0)
SELECT s.`month`, total, net_amount
FROM (SELECT MONTH(sale.`date`) AS `month`,
SUM(total) AS total
FROM sale
GROUP BY MONTH(sale.`date`)) s
LEFT JOIN (SELECT MONTH(receipt.`date`) AS `month`,
SUM(net_amount) AS net_amount
FROM receipt
GROUP BY MONTH(receipt.`date`)) r ON s.`month` = s.`month`
答案 1 :(得分:0)
试试这个
select Month(date),sum(total),sum(net_amount)
from (
select date,sum(total) as total,0 as net_amount from sale group by month(date)
union all
select date, 0 as total,sum(net_amount) from receipt group by month(date))
as tabl group by Month(date)