我有收据和支出的两个表,正在使用联合来合并它们并生成报告
SELECT MIN(date) AS trx, MAX(date), COUNT(*), SUM(amount), CONCAT(' - ','-'),
$f as _
FROM expenditure WHERE date >= '$start_date' AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
UNION
SELECT MIN(date) AS trx, MAX(date), COUNT(*),CONCAT(' - ','-'), SUM(amount),
$f as _
FROM receipts
WHERE date >= '$start_date' AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
ORDER BY trx
我得到以下结果
Array (
[0] => Array ( [trx] => 2012-03-06 [MAX(date)] => 2012-03-06 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-06 )
[1] => Array ( [trx] => 2012-03-08 [MAX(date)] => 2012-03-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-08 )
[2] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 2 [SUM(amount)] => 60000 [CONCAT(' - ','-')] => - - [_] => 2012-06-06 )
[3] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 1 [SUM(amount)] => - - [CONCAT(' - ','-')] => 487200 [_] => 2012-06-06 )
[4] => Array ( [trx] => 2012-06-08 [MAX(date)] => 2012-06-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-06-08 )
[5] => Array ( [trx] => 2012-06-29 [MAX(date)] => 2012-06-29 [COUNT(*)] => 2 [SUM(amount)] => 320000 [CONCAT(' - ','-')] => - - [_] => 2012-06-29 ) )
问题 如果您注意到 3 和 4,一天有两条记录。
[trx] => 2012-06-06
因为那天客户进行了贷记和借记。有没有办法只将像 3 和 4 这样的行组合成一行。SELECT DISTINCT dindnt 要么工作,要么 mysql 可以对同一日期出现的两个表中的记录求和。