3

第一个表(名为18_8_ChartOfAccounts),帐号如下

AccountNumber | VatReturnRowNumberForDebitTurnover
--------------------------------------------------
 1            |    not_blank
 2            |
 3            |    not_blank

第二张表(命名2_1_journal)像这样

Amount | DebitAccount
----------------------
5     |  1
80    |  2
3     |  3
4     |  1
20    |  3

试图获得以下信息:如果VatReturnRowNumberForDebitTurnover不为空/不为空,则 sum AmountwhereDebitAccount与 (equal to) 相同AccountNumber

或者想要得到这样的输出:

Account number | Total
---------------------------
1              | 9 (5+4)
3              | 23 (20+3)

不需要为 account 回显 Total 2,因为 account2 VatReturnRowNumberForDebitTurnover是空白/空的

这个 mysql 查询从第一个表中选择所有 AccountNumbers ( 18_8_ChartOfAccounts)

SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount 
FROM 18_8_ChartOfAccounts AS a 
LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount 
FROM ( SELECT * FROM 2_1_journal) DATA 
GROUP BY DebitAccount ) 
d ON (a.AccountNumber = d.DebitAccount)

但是只需要选择不是空白/空AccountNumber的sVatReturnRowNumberForDebitTurnover

尝试修改mysql查询

SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount 
FROM 18_8_ChartOfAccounts AS a 

WHERE VatReturnRowNumberForDebitTurnover <> ''

LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount 
FROM ( SELECT * FROM 2_1_journal) DATA 
GROUP BY DebitAccount ) 
d ON (a.AccountNumber = d.DebitAccount)

得到Syntax error or access violation: ... near 'LEFT JOIN ( SELECT DebitAccount ....

然后变了FROM 18_8_ChartOfAccounts AS a WHERE VatReturnRowNumberForDebitTurnover <> ''

FROM 18_8_ChartOfAccounts WHERE VatReturnRowNumberForDebitTurnover <> '' AS a得到类似的错误。

然后改成这样的代码:

SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount 
FROM 18_8_ChartOfAccounts AS a 
LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount 
FROM ( SELECT * FROM 2_1_journal) DATA 
WHERE a.VatReturnRowNumberForDebitTurnover <> "" 
GROUP BY DebitAccount ) 
d ON (a.AccountNumber = d.DebitAccount)

并得到Unknown column 'a.VatReturnRowNumberForDebitTurnover'

好的,在表中2_1_journal没有这样的列。希望以a.VatReturnRowNumberForDebitTurnover相同的方式使用a.AccountNumber(两列仅在18_8_ChartOfAccounts.a.AccountNumber作品中,a.VatReturnRowNumberForDebitTurnover不是。

请告知需要修改的内容

4

3 回答 3

3

这应该有效;

SELECT AccountNumber, 
       CONCAT(SUM(Amount), ' (', GROUP_CONCAT(Amount SEPARATOR '+'), ')') Total
FROM 18_8_ChartOfAccounts
LEFT JOIN 2_1_journal
  ON AccountNumber = DebitAccount
WHERE VatReturnRowNumberForDebitTurnover IS NOT NULL
GROUP BY AccountNumber

一个用于测试的 SQLfiddle

当然,如果 sum details 只是一个示例,您可以跳过该GROUP_CONCAT()部分并使用...

SELECT AccountNumber, COALESCE(SUM(Amount), 0) Total
...

另一个 SQLfiddle

于 2013-07-26T09:10:25.447 回答
2

只需使用此查询:

SELECT a.AccountNumber, SUM( b.Amount ) AS Amount
  FROM 18_8_ChartOfAccounts AS a 
     LEFT JOIN 2_1_journal AS b ON a.AccountNumber = b.DebitAccount
        WHERE a.VatReturnRowNumberForDebitTurnover IS NOT NULL
        GROUP BY b.DebitAccount;
于 2013-07-26T09:11:05.570 回答
1
select
ca.AccountNumber,
sum(j.Amount)
from
18_8_ChartOfAccounts ca
left join 2_1_journal j on ca.AccountNumber = j.DebitAccount
where 
ca.VatReturnRowNumberForDebitTurnover is not null
group by ca.AccountNumber

看到它在sqlfiddle中工作。

于 2013-07-26T09:11:37.920 回答