我有 2 个表需要根据日期和 2 个值加在一起。
这给了我所有信息的列表 - 很好。
$query = (SELECT date, debit, credit , note FROM proj3_cash )
UNION
(SELECT settle, purch, sale, issue FROM proj3_trades)
ORDER BY date";
现在我需要对两张表中的每日总计信息进行分组。
$query = "(SELECT date, SUM(debit), SUM(credit)FROM proj3_cash GROUP BY date)
UNION
(SELECT settle as date, SUM(purch) as debit, SUM(sale) as credit FROM proj3_trades GROUP BY date)
ORDER BY date";
很好,但是如果每个表中有相同日期的内容,我会得到:
date SUM(debit) SUM(credit)
--------------------------------------
2010-12-02 0.00 170.02
2010-12-02 296449.91 233111.10
如何将两者分组到同一天?
如果我在最后添加 GROUP BY - 我只会收到错误消息。还是应该通过 JOIN 来完成?